简体   繁体   English

如何使用Express-http-proxy使用Node代理以剥离最终的剩余部分

[英]How to Proxy using Node using express-http-proxy to strip final rest

On my local JavaScript, I want to make a rest call to 在本地JavaScript上,我想对

/rest/speakers

and have that proxied to 并将其代理给

http://localhost:2011/rest/speakers

I have code as follows that does not quite work as I want: 我有以下代码,但并不能完全满足我的要求:

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');
});

To make the proxy work, I actually need to call 要使代理正常工作,我实际上需要致电

/rest/rest/speakers

I kind of get it. 我有点明白。 It seems that I'm proxying my /rest to the root of localhost:2011 and then I need to dive into the /rest of that server. 看来我正在将/rest代理到localhost:2011的根目录,然后需要深入研究该服务器的/rest Adding /rest to the end of the proxy(..) is ignored. /rest添加到proxy(..)的末尾将被忽略。

Try using proxyReqPathResolver to modify your URL as needed. 尝试使用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;
    }
}));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM