关于在express中使用pipe导致请求参数丢失的问题


我在app.js中的中间件配置


 app.use(proxy.filter)
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

下面是proxy的代码:


 var request = require('request');
var config = require('../config');
const URL_TO_PROXY = [
    '/user/head/upload',
    '/user/head/get'
];
const PROXY_BASE_URL = config.proxyBaseUrl;
exports.filter = function(req,res,next) {
    if (URL_TO_PROXY.indexOf(req.path) != -1) {
        var reqUrl = PROXY_BASE_URL + req.path;
        req.pipe(request(reqUrl,function(error,response,body) {
            //console.log('代理返回的数据',body);
        })).pipe(res);
    } else {
        next();
    }
}

现行的服务器架构是这样的,浏览器请求服务器A,A通过pipe的方式代理访问到服务器B。
我发现使用req.pipe之后,B服务器收不到请求中的参数来,比如说我在前端请求/user/head/get?id=1这个地址,在B服务器上根本得不到这个id参数。

express.js request node.js pipe

反叛的撸修 9 years, 4 months ago

更改这一句:var reqUrl = PROXY_BASE_URL + req.path; 为var reqUrl = PROXY_BASE_URL + req.originalUrl; 问题解决

wyg老婆 answered 9 years, 4 months ago

Your Answer