如何使用express-http-proxy来使用Node来剥离最终休息

问题描述 投票:1回答:1

在我的本地JavaScript上,我想休息一下

/rest/speakers

并将其代理

http://localhost:2011/rest/speakers

我有如下代码,不能按我的意愿工作:

var proxy = require('express-http-proxy');
var app = require('express')();
app.use('/rest', proxy('localhost:2011/'));
app.listen(8081, function () {
    console.log('Listening on port 8081');
});

为了使代理工作,我实际上需要打电话

/rest/rest/speakers

我有点儿了。似乎我将我的/rest代理到localhost:2011的根,然后我需要潜入该服务器的/rest。将/rest添加到proxy(..)的末尾将被忽略。

node.js express npm http-proxy
1个回答
0
投票

尝试使用proxyReqPathResolver根据需要修改您的URL。

var url = require('url'),
    proxy = require('express-http-proxy');

// ... other app setup here 

app.use('/rest', proxy('localhost:2011', {
    proxyReqPathResolver: function(req, res) {
      return '/rest/rest' + url.parse(req.url).path;
    }
}));
© www.soinside.com 2019 - 2024. All rights reserved.