简体   繁体   English

尝试使用Node和Express-http-proxy代理并失败

[英]Trying to proxy with Node and express-http-proxy and failing

I'm launching NPM express in a very simple app and wanting to take what is passed into the URL and redirect it as follows. 我正在一个非常简单的应用程序中启动NPM Express,并希望将传递到URL中的内容按如下所示进行重定向。 Assuming I'm listening for web traffic on 8080 and I want to proxy my rest calls to port 5000. 假设我正在8080上监听网络流量,并且想将其余的呼叫代理到端口5000。

That is, when the URL http://localhost:3077/rest/speakers comes in, I want the results to come from http://localhost:5000/rest/speakers (where the word speakers could be sessions, attendees or any other name like that. 也就是说,当输入URL http:// localhost:3077 / rest / speakers时,我希望结果来自http:// localhost:5000 / rest / speakers (其中“说话者”一词可以是会议,参加者或任何这样的其他名字。

app = express();
app.use('/rest', proxy('http://localhost:5000/rest'));

app.listen(8080, function () {
  console.log('Listening on port 8080');
});

I seem to get the result of localhost:5000 but not even sure of that. 我似乎得到localhost:5000的结果,但甚至不确定。

I changed to using http-proxy-middleware and this solved my problem. 我改为使用http-proxy-middleware,这解决了我的问题。

app = express();
const proxy = require("http-proxy-middleware");
const targetVal = 'http://localhost:5000/rest';
app.use(proxy('/rest', {target: process.env.DEV_RESTURL, changeOrigin:true}));

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

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