我正在尝试使用express应用程序设置一个代理,指向我应用程序中特定路径的根路径:
http://my-domain.com/some/route --> http://another-domain:8000/对于每个http-proxy文档,我已经尝试了多种方法,但我总是遇到路径/路由的问题。我正尝试在一个已登录的express应用程序中这样做,这样我就可以在我试图代理的应用程序后面使用我的身份验证。我一直收到代理程序的错误消息,说路径‘/ defined...etc /route’不是defined...etc。
var httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer({});
proxy.proxyRequest(req, res, {
host:'localhost',
port:8000
});我也尝试过:
var url = 'http://localhost:8000/';
var httpProxy = require('http-proxy'),
proxy = httpProxy.createProxyServer({});
proxy.web(req,res, { target: url }, function(e) {
console.log('proxy.web callback');
console.log(e);
});该函数调用,但我以EXPRESS404错误结束...
如果可能的话,我还想传入一些变量,例如:
http://my-domain.com/some/route?var1=something&var2=something --> http://another-domain:8000/?var1=something&var2=something但不知道这是否可能,我尝试在请求上设置它,因为它被发送到proxyRequest,但在第二个应用程序中找不到它们。
发布于 2014-10-14 18:54:12
嗯,我遇到了另一个问题,但需要先解决这个问题。我想出了这个代码,它对我来说工作得很好;)
只需将其用于"/some/route“
.... // your stuff
httpProxy.on('error', function (err, req, res) {
res.writeHead(500, {
'Content-Type': 'text/plain'
});
res.end('some error');
});
app.all( '/some/route/*' , function( req , res ) {
var url = req.url;
url = url.slice(11); // to remove "/some/route"
req.url = url;
return httpProxy.web(req, res , { target: "http://another-domain:8000" } );
} );希望这能有所帮助。
https://stackoverflow.com/questions/26002177
复制相似问题