[英]Node hangs on POST request when using http-proxy and body-parser with express
I've also posted this to the relevant issue on http-proxy
.我还在http-proxy
上将其发布到相关问题上。
I'm using http-proxy
with express
so I can intercept requests between my client and api in order to add some cookies for authentication.我将http-proxy
与express
一起使用,因此我可以拦截客户端和 api 之间的请求,以便添加一些用于身份验证的 cookie。
In order to authenticate the client must send a POST request with x-www-form-urlencoded
as the the content-type.为了验证客户端必须发送一个带有x-www-form-urlencoded
作为内容类型的 POST 请求。 So I am using body-parser
middleware to parse the request body so I can insert data in the request.所以我使用body-parser
中间件来解析请求正文,以便我可以在请求中插入数据。
http-proxy
has a problem with using body-parser
supposedly because it parses the body as a stream and never closes it so the proxy never never completes the request. http-proxy
在使用body-parser
有问题,因为它将 body 解析为流并且从不关闭它,因此代理永远不会完成请求。
There is a solution in the http-proxy examples that "re-streams" the request after it has been parsed which I have tried to use. http-proxy 示例中有一个解决方案,可以在我尝试使用的解析后“重新流式传输”请求。 I have also tried to use the connect-restreamer
solution in the same issue with no luck.我还尝试在同一问题中使用connect-restreamer
解决方案,但没有成功。
My code looks like this我的代码看起来像这样
var express = require('express'),
bodyParser = require('body-parser'),
httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer({changeOrigin: true});
var restreamer = function (){
return function (req, res, next) { //restreame
req.removeAllListeners('data')
req.removeAllListeners('end')
next()
process.nextTick(function () {
if(req.body) {
req.emit('data', req.body) //error gets thrown here
}
req.emit('end')
})
}
}
var app = express();
app.use(bodyParser.urlencoded({extended: false, type: 'application/x-www-form-urlencoded'}));
app.use(restreamer());
app.all("/api/*", function(req, res) {
//modifying req.body here
//
proxy.web(req, res, { target: 'http://urlToServer'});
});
app.listen(8080);
and I receive this error我收到这个错误
/Code/project/node_modules/http-proxy/lib/http-proxy/index.js:119
throw err;
^
Error: write after end
at ClientRequest.OutgoingMessage.write (_http_outgoing.js:413:15)
at IncomingMessage.ondata (_stream_readable.js:540:20)
at IncomingMessage.emit (events.js:107:17)
at /Code/project/lib/server.js:46:25
at process._tickCallback (node.js:355:11)
I have tried to debug the flow but am grasping for straws.我试图调试流程,但我正在抓紧稻草。 Any suggestions please??请问有什么建议吗??
I was experiencing this issue and I was not able to get restreaming to work. 我遇到了这个问题,我无法恢复工作。 Here is the solution I came to albeit crude and may not be acceptable for your project. 这是我遇到的解决方案,虽然粗糙,可能不适合您的项目。
I ended up moving the body-parser middleware on to the route itself and not be route middleware since my project only had a couple routes and the reset were going through my http-proxy middleware. 我最终将身体解析器中间件移动到路由本身而不是路由中间件,因为我的项目只有几条路由并且重置是通过我的http-proxy中间件。
so instead of this: 所以不要这样:
router.use(bodyParser.json());
router.get('/', function(){...});
router.use(httpProxy());
I did this: 我这样做了:
router.get('/', bodyParser.json(), function(){...})
router.use(httpProxy())
I was facing this same issue with http-proxy-middleware
.我在使用http-proxy-middleware
遇到了同样的问题。 After reading @ChrisMckenzie's answer, I decided to simply move the body parser middleware to after the proxy middleware.阅读@ChrisMckenzie 的回答后,我决定简单地将正文解析器中间件移到代理中间件之后。
so instead of this:所以而不是这个:
router.use(bodyParser.json());
router.get('/', function(){...});
router.use(httpProxy());
I did this:我这样做了:
router.use(httpProxy());
router.use(bodyParser.json());
router.get('/', function(){...});
http-proxy is notoriously bad at handling POST bodies, especially in the latest versions of Node; http-proxy在处理POST主体时非常糟糕,特别是在最新版本的Node中; and middleware hacks don't always work with all POST requests. 和中间件黑客并不总是适用于所有POST请求。 I suggest you use a dedicated http proxy engine like the one in NGINX or HAPROXY, those work the best. 我建议你使用像NGINX或HAPROXY那样的专用http代理引擎,这些引擎效果最好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.