Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用请求模块的Node.JS代理_Node.js_Express_Proxy_Request - Fatal编程技术网

使用请求模块的Node.JS代理

使用请求模块的Node.JS代理,node.js,express,proxy,request,Node.js,Express,Proxy,Request,我有一个Express.JS应用程序,它使用请求节点模块对路由进行代理调用。这在NodeJS V0.10.28中运行良好;但是,升级到NodeJS V4.4.7导致此操作失败——抛出一个错误“error:write after end” 我对NodeJS是新手;所以我感谢你的帮助 var bodyParser=require('body-Parser'); app.use(bodyParser.json({limit: '100mb'})); app.use(bodyParser.urlenco

我有一个Express.JS应用程序,它使用请求节点模块对路由进行代理调用。这在NodeJS V0.10.28中运行良好;但是,升级到NodeJS V4.4.7导致此操作失败——抛出一个错误“error:write after end”

我对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);   
});

对于子孙后代,这是我的解决方案,以防其他人也会面临同样的问题

问题在于中间件主体解析器的使用:主体解析器读取请求直至完成,因此流已到达其终点。因此,问题中显示的/relay路由中的req.pipe()没有任何内容可读取,并且不会重新启动流

不过,返回的错误消息“结束后写入”非常模糊。我为什么没有在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}));

对于子孙后代,这是我的解决方案,以防其他人也会面临同样的问题

问题在于中间件主体解析器的使用:主体解析器读取请求直至完成,因此流已到达其终点。因此,问题中显示的/relay路由中的req.pipe()没有任何内容可读取,并且不会重新启动流

不过,返回的错误消息“结束后写入”非常模糊。我为什么没有在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}));