javascript 向 Node-http-proxy Node.js 发布请求时套接字挂断
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26632854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Socket hangup while posting request to Node-http-proxy Node.js
提问by Tareq Salah
I'm working in Node.js Project and I want node to act as a proxy for solr
我在 Node.js 项目中工作,我希望节点充当 solr 的代理
For proxy: I used Node-http-proxy. the problem is that proxy work excellent in case of get requests but in case of post requests it results in socket hang up exception
对于代理:我使用了Node-http-proxy。问题是代理在获取请求的情况下工作得很好,但在发布请求的情况下会导致套接字挂断异常
Here is a sample of my node code
这是我的节点代码示例
var express = require('express');
var router = express.Router();
var http = require('http');
var httpProxy = require('http-proxy')
var proxyOptions = {
host: "127.0.0.1",
port: 8983
};
var proxy = httpProxy.createProxyServer(proxyOptions);
// It works excellent in GET request
router.get('/solr/*', function(req, res) {
proxy.web(req, res, {
target: 'http://' + proxyOptions.host + ':' + proxyOptions.port
});
})
// the socket hang up in post request
router.post('/solr/*', function(req, res) {
console.log('Post Request');
proxy.web(req, res, {
target: 'http://' + proxyOptions.host + ':' + proxyOptions.port
});
})
And this is the error after some time in node console
这是节点控制台一段时间后的错误
Error: socket hang up
at createHangUpError (http.js:1476:15)
at Socket.socketOnEnd [as onend] (http.js:1572:23)
at Socket.g (events.js:180:16)
at Socket.emit (events.js:117:20)
at _stream_readable.js:943:16
at process._tickCallback (node.js:419:13)
Any suggestions about the cause of the problem
关于问题原因的任何建议
采纳答案by Hazem Hagrass
I think the issue comes from the order of middleware. Using bodyParser before httpProxy will break the requests with JSON body, so httpProxy should be used before bodyParser.
我认为问题来自中间件的顺序。在 httpProxy 之前使用 bodyParser 会破坏带有 JSON body 的请求,因此应该在 bodyParser 之前使用 httpProxy。
You may want to check thisfor more info about bodyParser.
您可能要检查这个约bodyParser更多信息。
回答by Ricky
Use a callback to listen for the error:
使用回调来监听错误:
proxy.web(req, res, { target: 'http://mytarget.com:8080' }, function(e) { ... });
回答by Tareq Salah
I found the solution to this problem with the help of this issue https://github.com/nodejitsu/node-http-proxy/issues/180#issuecomment-12244852
我在这个问题的帮助下找到了这个问题的解决方案 https://github.com/nodejitsu/node-http-proxy/issues/180#issuecomment-12244852
the solution is to use middleware for proxy before using the bodyparser
解决方案是在使用bodyparser之前使用中间件进行代理
code sample
代码示例
// use middleware first
app.post('/solr/*',function(req, res) {
console.log('POST REQUEST')
//res.end();
proxy.web(req, res, {
target: 'http://' + proxyOptions.host + ':' + proxyOptions.port
});
})
app.use(logger('dev'));
// use bodyparser after that
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));