[英]Proxy target route to invalid url
I am trying to get http-proxy-middleware working, what I am trying to achieve is having multiple nodejs apps running and then having the proxy app as a reverse proxy server. 我试图让http-proxy-middleware正常工作,我想要实现的是运行多个nodejs应用程序,然后将代理应用程序作为反向代理服务器。
The problem I encounter is when I press a link on a proxied app, here's my code: 我遇到的问题是当我按下代理应用程序上的链接时,这是我的代码:
var express = require('express')
var proxy = require('http-proxy-middleware')
var app = express()
var options = {
target: 'http://localhost:1001', // target host
changeOrigin: true, // needed for virtual hosted sites
ws: false, // proxy websockets
logLevel: "debug",
pathRewrite: {
'^/foo': '/', // rewrite path
}
}
var exampleProxy = proxy(options)
app.use('/foo', exampleProxy)
app.listen(3000)
On the localhost:1001 app I got the routes '/' and '/bar' 在localhost:1001 app我得到了路线'/'和'/ bar'
app.get('/', function (req, res) {
res.render('home');
});
app.get('/bar', function (req, res) {
res.render('bar');
});
If I go to localhost:3000/foo it will reroute me to localhost:1001/ (while showing localhost:3000/ in the browser) and the same with localhost:3000/foo/bar. 如果我去localhost:3000 / foo它会将我重新路由到localhost:1001 /(同时在浏览器中显示localhost:3000 /)和localhost:3000 / foo / bar相同。 So this works fine.
所以这很好。
The problem happens when I go to localhost:3000/foo and then press the link to 'bar', it will then route me to localhost:3000/bar which is not a route I have defined in my proxy server. 当我转到localhost:3000 / foo然后按下“bar”链接时会出现问题,它会将我路由到localhost:3000 / bar,这不是我在代理服务器中定义的路由。
So what I need is that when I press on the link to /bar it will route it to /foo/bar in the proxy. 所以我需要的是,当我按下链接到/ bar时,它会将它路由到代理中的/ foo / bar。
I have tried coming up with some ways to fix this, but have (obviously) not been succesful: 我已经尝试了一些方法来解决这个问题,但(显然)没有成功:
Add the port (1001) to the response (res.locals.portNo = "1001") and then send that in the request so that the proxy can check where the request is coming from and add /foo (if its 1001). 将端口(1001)添加到响应(res.locals.portNo =“1001”),然后在请求中发送它,以便代理可以检查请求的来源并添加/ foo(如果是1001)。 (I have not tried this yet, but maybe this could be achieved by using cookies?)
(我还没有尝试过,但也许这可以通过使用cookies来实现?)
I got it working but it is a very bad hack... But it works... 我得到了它的工作,但这是一个非常糟糕的黑客......但它的工作原理......
I added cookie-parser 我添加了cookie-parser
var cookieParser = require('cookie-parser')
app.use(cookieParser())
And then I added this ugly middleware 然后我添加了这个丑陋的中间件
app.use(function (req, res, next) {
var domainLetter = req.cookies.domainLetter
switch (req.url.substring(0, 2)) {
case "/a": {
domainLetter = 'a'
res.cookie('domainLetter', 'a')
break
}
case "/b": {
domainLetter = 'b'
res.cookie('domainLetter', 'b')
break
}
default: {
break
}
}
if (req.url.length != 2 || req.url.substring(0, 2) != '/' + domainLetter)
req.url = '/' + domainLetter + req.url
next()
})
So what I am doing is when I want to use the proxy for localhost:1001, I go to localhost:3000/a (or localhost:3000/a/****), it will then save a cookie with the property "domainLetter" with the value "a", then whenever I click a link on that website, it will add "/a" to that url (localhost:3000/test becomes localhost:3000/a/test etc) 所以我正在做的是当我想使用localhost的代理时:1001,我转到localhost:3000 / a(或localhost:3000 / a / ****),然后它将保存带有属性的cookie“ domainLetter“的值为”a“,然后每当我点击该网站上的链接时,它会将”/ a“添加到该网址(localhost:3000 / test成为localhost:3000 / a / test等)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.