您的位置:首页 > 理论基础 > 计算机网络

用nodejs http-server搭建本地服务环境

2015-09-10 08:59 603 查看
一、本地环境,不包括域名的方法:
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,返回的结果当然肯定会根据你的代理服务器不同有所不同。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: