node-http-proxy POST 请求超时

node-http-proxy POST request times out

我正在为 POST 请求使用 node-http-proxy,如下所示:

route.js
---------

var express = require('express');
var httpProxy = require('http-proxy');
var bodyParser = require('body-parser');
var proxy = httpProxy.createProxyServer({secure:false});
var jsonParser = bodyParser.json();

proxy.on('proxyReq', function(proxyReq, req, res, options) {
    logger.debug("proxying for",req.url);
    //set headers
    logger.debug('proxy request forwarded succesfully');
});

proxy.on('error', function (err, req, res) {
  res.writeHead(500, {
    'Content-Type': 'text/plain'
  });
  res.end('Something went wrong. And we are reporting a custom error message.');
});

proxy.on('proxyRes', function (proxyRes, req, res) {
  console.log('RAW Response from the target', JSON.stringify(proxyRes.headers, true, 2));
});

module.exports = function(app){
  app.post('/recording',jsonParser,function(req,res){
    // update request body
    proxy.web(req, res, { target: <<host>>:<<port>>});
  });
}

app.js
---------

var express = require('express');
var app = express();
 require('./routes')(app);

app.listen(8080);
console.log("Demo server running");

我也使用 bodyparser 中间件,它有一个已知问题,如 Gitbug issue 中所述。所以我尝试将这一行添加为 app.js

中的最后一行
app.use(require('connect-restreamer')());

但 POST 请求仍然挂起并最终失败。我该如何解决 ? bodyparser 有其他替代品吗?

尝试颠倒 bodyParser 和代理中间件的顺序:

module.exports = function(app){
  app.post('/recording', function(req,res){
    // update request body
    proxy.web(req, res, { target: <<host>>:<<port>>});
  }, jsonParser);
}

认为此问题类似于:socket hang up error with nodejs

稍微扩展一下,这里发生的是节点请求是一个流,它只能被读取一次,之后流数据被消费。

当您在 express 中使用 body-parser 中间件时,它会消耗请求流主体 - 如果您在此之后尝试代理请求,则没有要发送的主体流,因此代理的另一端会收到一个 POST 具有内容长度等...但无限期地等待接收从未到达的 POST 主体。

如果您想要代理 POST/PUT 或任何包含主体的请求,您必须在任何中间件使用主体之前执行此操作。这就是为什么@chimmurai 上面的回答有效。

另外,请注意,出于同样的原因,代理请求后执行的中间件将受到同样的影响,一旦请求流被消耗,后续中间件将无法读取任何内容。这就是 connect-restreamer 之类的原因。