pul*_*hal 6 http-proxy node.js node-http-proxy
这是我正在使用的模块版本:
$ npm list -g | grep proxy
??? http-proxy@0.10.0
Run Code Online (Sandbox Code Playgroud)
一个webservice调用我的机器,我的任务是根据请求主体的内容将请求代理到另一个URL和主机,并附加一个查询参数:
var http = require('http'),
httpProxy = require('http-proxy')
form2json = require('form2json');
httpProxy.createServer(function (req, res, proxy) {
// my custom logic
var fullBody = '';
req.on('data', function(chunk) {
// append the current chunk of data to the fullBody variable
fullBody += chunk.toString();
});
req.on('end', function() {
var jsonBody = form2json.decode(fullBody);
var payload = JSON.parse(jsonBody.payload);
req.url = '/my_couch_db/_design/ddoc_name/_update/my_handler?id="' + payload.id + '"';
// standard proxy stuff
proxy.proxyRequest(req, res, {
changeOrigin: true,
host: 'my.cloudant.com',
port: 443,
https: true
});
});
}).listen(8080);
Run Code Online (Sandbox Code Playgroud)
但我一直遇到如下错误: An error has occurred: {"code":"ECONNRESET"}
有人知道需要修复什么吗?
这对我有用:
var httpProxy = require('http-proxy');
var options = {
changeOrigin: true,
target: {
https: true
}
}
httpProxy.createServer(443, 'www.google.com', options).listen(8001);
Run Code Online (Sandbox Code Playgroud)
要将端口上传入的所有请求转发3000到https://google.com:
const https = require('https')
const httpProxy = require('http-proxy')
httpProxy.createProxyServer({
target: 'https://google.com',
agent: https.globalAgent,
headers: {
host: 'google.com'
}
}).listen(3000)
Run Code Online (Sandbox Code Playgroud)
示例灵感来自https://github.com/nodejitsu/node-http-proxy/blob/master/examples/http/proxy-http-to-https.js。