【发布时间】:2015-06-10 13:32:00
【问题描述】:
我正在使用nodejs、expressjs和request来创建反向代理。
我想要的是当一个请求被发送到 http://localhost:PORT/NAME/ 相应的页面已加载。
例如,如果我输入 http://localhost:1234/google/ 谷歌网站将被加载 如果我输入 http://localhost:1234/stack/ 而是加载 stackoverflow 页面。
我尝试了类似于这里的代码
var request = require('request');
app.get('/google', function(req,res) {
//modify the url in any way you want
var newurl = 'http://google.com/';
request(newurl).pipe(res);
});
问题是,它会加载页面,但不是完全加载,因为页面本身想要加载图像和 css 以及其他内容并发出 /images/example.jpg 之类的请求 而那些显然以 404 错误结束。
有没有人为这个问题提供方便的解决方案? 特别是因为简单地为 /images 设置路由并不好,因为我有两个可能的目标服务器,而代理不知道哪个是正确的。
【问题讨论】:
标签: javascript node.js proxy reverse-proxy