简体   繁体   English

使用带有 express 的 http-proxy 和 body-parser 时,节点挂起 POST 请求

[英]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-proxyexpress一起使用,因此我可以拦截客户端和 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.

相关问题 Express.Router、body-parser 和 post 请求正文 - Express.Router, body-parser and post request body 如何使用http-proxy获取请求的正文? - How to get request's body using http-proxy? 通过 AJAX (Vanilla JavaScript) 的 POST 请求在 Node.js (express/body-parser/ejs) 上生成空对象 - POST request via AJAX (Vanilla JavaScript) produce empty object on Node.js (express/body-parser/ejs) 无法使用node.js v0.12.7检索POST数据,表示为4,正文解析器,引导程序3 - Can't retrieve POST data using node.js v0.12.7, express 4, body-parser, bootstrap 3 Express.js + body-parser:POST请求中的空req.body - Express.js + body-parser: empty req.body from POST request Node.js/Express.js 请求正文未定义。 body-parser 已安装并导入 - Node.js/Express.js request body is undefined. body-parser is installed and imported 使用express和body-parser在服务器端解析AngularJS http.post数据 - Parsing AngularJS http.post data on server side with express & body-parser 如何通过 Body-parser 获取 post 值 - Express Node.js - How to get post values by Body-parser - Express Node.js 你如何使用mongodb native,express和body-parser发布请求并保存数据? - How do you use mongodb native, express and body-parser to post request and save data? 由于 CORS 问题,AJAX POST 未使用 body-parser 向 express.js 服务器发送请求(已解决) - AJAX POST not sending request to express.js server with body-parser due CORS problem (SOLVED)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM