zoukankan      html  css  js  c++  java
  • 用nodejs http-server搭建本地服务环境

    一、本地环境,不包括域名的方法:

    https://www.npmjs.com/package/http-server(参考手册)

    正确使用方法:

    二、带域名的配置方法:

    https://cnodejs.org/topic/50d41da5637ffa4155f63179和http://blog.csdn.net/yuan882696yan/article/details/25052469(参考方法)

    nodejs通过代理(proxy)发送http请求(request)

    方法一:

    request.post({url:url,proxy:'http://username:password@kws.proxy.nic.fujitsu.com:8080',oauth:oauth} 

    var options = {
            host: "kws.proxy.nic.fujitsu.com",
            port: 8080,
            path: requestPath,
    headers: {
       'Proxy-Authentication': 'Base ' + new Buffer('username:password').toString('base64')
       }
     };


    var reqUrl = request(url,{'proxy':'http://username:password@kws.proxy.nic.fujitsu.com:8080'});

    注意:用户名,密码在url中的写法

    方法二:

    有可能有这样的需求,需要node作为web服务器通过另外一台http/https代理服务器发http或者https请求,废话不多说直接上代码大家都懂的:

    var http = require('http')
    var opt = {
     host:'这里放代理服务器的ip或者域名',
     port:'这里放代理服务器的端口号',
     method:'POST',//这里是发送的方法
     path:' https://www.google.com',     //这里是访问的路径
     headers:{
      //这里放期望发送出去的请求头
     }
    }
    //以下是接受数据的代码
    var body = '';
    var req = http.request(opt, function(res) {
      console.log("Got response: " + res.statusCode);
      res.on('data',function(d){
      body += d;
     }).on('end', function(){
      console.log(res.headers)
      console.log(body)
     });
    
    }).on('error', function(e) {
      console.log("Got error: " + e.message);
    })
    req.end();

    这样我们就通过了指定代理服务器发出了https的请求,注意这里我们同代理服务器是http协议的,不是https,返回的结果当然肯定会根据你的代理服务器不同有所不同。

  • 相关阅读:
    cookie和session的区别
    PHP中require和include的区别
    设计模式之建造者模式
    设计模式之抽象工厂模式
    设计模式之工厂模式
    HTTPS为什么是安全的?
    设计模式之单例模式(Singleton Pattern)
    设计模式(Design Patterns)
    Linux命令:awk
    Nginx与PHP如何协同工作
  • 原文地址:https://www.cnblogs.com/dearxinli/p/4796769.html
Copyright © 2011-2022 走看看