【发布时间】:2016-01-25 13:47:56
【问题描述】:
在 Node 上使用 connect 库,我尝试在使用 node-http-proxy 代理之前检索请求正文。
从 Node v4 开始,我们必须使用一些中间件,如 bodyParser(或只是 data/end 请求事件)来检索 POST 请求正文。
问题是,似乎消耗请求流,被代理时请求超时。
这里是代码,首先我用 data 事件检索正文,然后将它交给 http-proxy,但请求超时。
var httpProxy = require('http-proxy');
var connect = require('connect');
var proxy = new httpProxy.createProxyServer({
target: {
host: 'localhost',
port: 9015
}
});
var app = connect()
.use(function (req, res, next) {
var bodyBuffer = '';
req.on('data', function (data) {
bodyBuffer += data;
});
req.on('end', function () {
req.body = buffer;
next();
});
})
.use(function (req, res) {
//I can use req.body now
//But the proxy request timeouts
proxy.web(req, res);
});
http.createServer(app).listen(3000);
【问题讨论】:
标签: node.js connect node-http-proxy