Node.js 节点http代理POST请求超时
我对POST请求使用节点http代理,如下所示:Node.js 节点http代理POST请求超时,node.js,proxy,node-http-proxy,body-parser,Node.js,Proxy,Node Http Proxy,Body Parser,我对POST请求使用节点http代理,如下所示: 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()
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");
但POST请求仍然挂起并最终失败。我该如何解决这个问题?bodyparser有其他选择吗?尝试颠倒bodyparser和代理中间件的顺序:
module.exports = function(app){
app.post('/recording', function(req,res){
// update request body
proxy.web(req, res, { target: <<host>>:<<port>>});
}, jsonParser);
}
module.exports=函数(应用程序){
应用程序post('/recording',函数(req,res){
//更新请求正文
web(req,res,{target::});
},jsonParser);
}
请认为此问题类似于:。尝试颠倒bodyParser和代理中间件的顺序:
module.exports = function(app){
app.post('/recording', function(req,res){
// update request body
proxy.web(req, res, { target: <<host>>:<<port>>});
}, jsonParser);
}
module.exports=函数(应用程序){
应用程序post('/recording',函数(req,res){
//更新请求正文
web(req,res,{target::});
},jsonParser);
}
认为这个问题类似于:。app.js中的
app
实例与routes.js中的相同吗?@KevinReilly是的,它是相同的实例。我从app.js本身传递它,并用实际来源更新了问题。app.js中的app
实例与routes.js中的相同吗?@KevinReilly是的,它是相同的实例。我从app.js本身传递它,并且我已经用实际的来源更新了问题。当我收到一个“/录制”的POST请求时,我需要从请求主体解析JSON,然后将其代理到另一台服务器。当我在代理之后移动中间件时,我无法从初始请求中提取JSON,因为它表示未定义;因为bodyParser把它们搞乱了。当我收到“/录制”的POST请求时,我需要从请求主体解析JSON,然后将其代理到另一台服务器。当我在代理之后移动中间件时,我无法从初始请求中提取JSON,因为它表示未定义;因为bodyParser把它们搞乱了。