套接字挂起与nodejs的错误

我想为某个XYZ服务器设置代理服务器,但我不是pipe理员。 然后,我想对请求和响应头进行一些分析,然后通过请求和响应主体进行分析。 所以我使用http代理https://github.com/nodejitsu/node-http-proxy

这是我正在做的事情:

var proxy = httpProxy.createProxyServer(); connect.createServer( connect.bodyParser(), require('connect-restreamer')(), function (req, res) { proxy.web(req, res, { target : 'XYZserver' }); } ).listen(config.listenPort); 

高达GET请求一切都很好,但每当有一些像POST,PATCH,PUT等请求的身体的请求,我得到的错误:

 Error: socket hang up at createHangUpError (http.js:1472:15) at Socket.socketCloseListener (http.js:1522:23) at Socket.EventEmitter.emit (events.js:95:17) at TCP.close (net.js:466:12) 

我谷歌了很多,但没有发现什么是错误的线索。 我使用“proxy.web”的“ws:true”选项启用套接字代理,但仍然是相同的错误。

我有一个类似的问题,并通过将我的代理代码移到其他节点中间件上来解决它。

例如:

 var httpProxy = require('http-proxy'); var apiProxy = httpProxy.createProxyServer(); app.use("/someroute", function(req, res) { apiProxy.web(req, res, { target: 'http://someurl.com'}) }); app.use(someMiddleware); 

不是这个:

 app.use(someMiddleware); var httpProxy = require('http-proxy'); var proxy = httpProxy.createProxyServer(); app.use("/someroute", function(req, res) { proxy.web(req, res, { target: 'http://someurl.com'}) }); 

我的具体问题是在代理之上有BodyParser中间件。 我没有做太多的挖掘工作,但是它一定是以某种方式修改了这个请求,这个请求终于来到了这个地方,这个请求破坏了代理库。

需要捕获代理的错误:

 var httpProxy = require('http-proxy'); var proxy = httpProxy.createProxyServer({ target: argv.proxy_url }) proxy.on('error', function(err, req, res) { res.end(); }) 

为了完整起见,在这个线程中,真正存在模块body-parserhttp-proxy之间的集成问题。

如果你不能改变中间件的顺序, 您可以在代理请求之前重新分析parsing的主体。

 // restream parsed body before proxying proxy.on('proxyReq', function(proxyReq, req, res, options) { if (req.body) { let bodyData = JSON.stringify(req.body); // incase if content-type is application/x-www-form-urlencoded -> we need to change to application/json proxyReq.setHeader('Content-Type','application/json'); proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData)); // stream the content proxyReq.write(bodyData); } } 

我已经在这个问题上失去了2天。 希望能帮助到你!

看起来, http-proxybody-parser中间件不兼容。 您可能希望在body-parser之前移动http-proxy中间件,或停止使用body-parser

另请参见:将请求发送到Node-http-proxy Node.js时套接字挂断

在浪费了一天多的时间后,在nodejitsu / node-http-proxy问题中发现了一些post,我可以通过riccardo.cardin使它工作。 我决定发布完整的例子来节省您的时间。 下面的例子使用服务器表示 , 身体分析器 (req.body中间件)和当然HTTP代理代理和转发请求到第三方服务器。

 const webapitargetUrl = 'https://posttestserver.com/post.php'; var bodyParser = require('body-parser'); app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.json()); // support json encoded bodies var https = require('https'); var stamproxy = httpProxy.createProxyServer({ target: 'https://localhost:8888', changeOrigin: true, agent : https.globalAgent, toProxy : true, secure: false, headers: { 'Content-Type': 'application/json' } }); stamproxy.on('proxyReq', function(proxyReq, req, res, options) { console.log("proxying for",req.url); if (req.body) { console.log("prxyReq req.body: ",req.body); // modify the request. Here i just by removed ip field from the request you can alter body as you want delete req.body.ip; let bodyData = JSON.stringify(req.body); // in case if content-type is application/x-www-form-urlencoded -> we need to change to application/json proxyReq.setHeader('Content-Type','application/json'); proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData)); // stream the content console.log("prxyReq bodyData: ",bodyData); proxyReq.write(bodyData); } console.log('proxy request forwarded succesfully'); }); stamproxy.on('proxyRes', function(proxyRes, req, res){ proxyRes.on('data' , function(dataBuffer){ var data = dataBuffer.toString('utf8'); console.log("This is the data from target server : "+ data); }); }); app.use(compression()); app.use(favicon(path.join(__dirname, '..', 'static', 'favicon.ico'))); app.use(Express.static(path.join(__dirname, '..', 'static'))); var sessions = require("client-sessions"); app.use(sessions({ secret: 'blargadeeblargblarg', cookieName: 'mysession' })); app.use('/server/setserverip', (req, res) => { console.log('------------ Server.js /server/setserverip ---------------------------------'); req.mysession.serverip += 1; console.log('session data:'); console.log(req.mysession.serverip) console.log('req.body:'); console.log(req.body); // Proxy forwarding stamproxy.web(req, res, {target: webapitargetUrl}); console.log('After calling proxy serverip'); });