我有一个Express.JS应用程序,该应用程序使用请求节点模块对路由进行代理调用。在NodeJS V0.10.28上运行良好。但是,升级到NodeJS V4.4.7导致失败—引发错误“错误:结束后写入”。
我是NodeJS的新手。非常感谢您的帮助。
var bodyParser=require('body-Parser');
app.use(bodyParser.json({limit: '100mb'}));
app.use(bodyParser.urlencoded({extended: false}));
....
....
app.use('/relay', function (req, res) {
var request = require('request'),
proxyUrl = 'http://abc.proxy.xyz:12345',
apiEndPoint = "https://aaa.bbb.ccc/svc";
req.pipe(request.post(apiEndPoint,{ proxy: proxyUrl, form: req.body}, function (error, response, body) {
if (error) {
console.log(error)
} else {
console.log("No error here.")
}
res.end();
})).pipe(res);
});
为了后代,这是我的解决方案,以防其他人遇到同样的问题。
问题出在使用中间件body-parser:body-parser读取请求以完成操作-因此流已到达末尾。因此,问题中显示的/ relay路由中的req.pipe()没有任何要读取的内容,并且不会重新启动流。
但是,返回的错误消息“ end after after”非常模棱两可。为什么我没有遇到Node.JS V0.10.28这个问题也是一个谜。在V4.4.7中注意到了所报告的问题
因此,要解决body-parser问题,我将/ relay路由移到了对body-parser的调用之上:
app.use('/relay', function (req, res) {
var request = require('request'),
proxyUrl = 'http://abc.proxy.xyz:12345',
apiEndPoint = "https://aaa.bbb.ccc/svc";
req.pipe(request.post(apiEndPoint,{ proxy: proxyUrl, form: req.body}, function (error, response, body) {
if (error) {
console.log(error)
} else {
console.log("No error here.")
}
res.end();
})).pipe(res);
});
....
....
var bodyParser=require('body-Parser');
app.use(bodyParser.json({limit: '100mb'}));
app.use(bodyParser.urlencoded({extended: false}));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句