用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请求,废话不多说直接上代码大家都懂的:
这样我们就通过了指定代理服务器发出了https的请求,注意这里我们同代理服务器是http协议的,不是https,返回的结果当然肯定会根据你的代理服务器不同有所不同。
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,返回的结果当然肯定会根据你的代理服务器不同有所不同。
相关文章推荐
- UI中的网络请求
- 《TCP/IP详解卷1:协议》——第5章 RARP:逆地址解析协议(转载)
- 《TCP/IP详解卷1:协议》——第4章 ARP:地址解析协议(转载)
- 为ElasticSearch添加HTTP基本认证
- 封装网络请求
- 稳定高效的MySQL与MSSQL读写分离/连接复用/Sharding-把HTTP的技术推进到数据层
- 对开源C++网络库简单应用总结
- 计算机网络之Socket
- Android http连接设置超时无效
- HttpServletResponse对象
- Linux TCP/IP大合集
- 如何在 Linux 中快速地通过 HTTP 提供文件访问服务
- 【github】2015.09.09 linux 命令行设置代理,连接github(Linux command line set http proxy for connecting github)
- Cocos2dx引擎 如何使用HttpClient
- TCP/IP 协议 建立与链接
- Uva 12549 Sentry Robots 网络流 - 二分图匹配
- linux之网络管理命令
- 大数据网络诊断系统项目总结
- new和newInstance的区别
- 网络流例题及构图选讲