我需要能够通过我的节点服务器提供副本站点(到www.google.com,www.facebook.com等任何站点).我找到了这个库:
https://github.com/nodejitsu/node-http-proxy
代理请求时我使用了以下代码:
options = {
ignorePath: true,
changeOrigin: false
}
var proxy = httpProxy.createProxyServer({options});
router.get(function(req, res) {
proxy.web(req, res, { target: req.body.url });
});
Run Code Online (Sandbox Code Playgroud)
但是,此配置会导致大多数站点出错.根据网站的不同,我会收到Unknown service来自目标网址的错误,或者来自Invalid host这些行的错误信息.但是,当我通过
changeOrigin: true
Run Code Online (Sandbox Code Playgroud)
我得到了一个正常运行的代理服务,但我的用户浏览器被重定向到他们请求的实际网址,而不是我的(因此,如果req.body.url = http://www.google.com请求将转到http://www.google.com)
我怎样才能使我的网站的网址显示出来,但是我可以准确地复制正在显示的内容?我需要能够在请求中添加一些JS文件,我正在使用另一个库.
为了澄清,这里是问题的摘要:
用户请求具有url属性的资源
这url是以.的形式http://www.example.com
我的服务器运行www.pv.com,需要能够指导用户www.pv.com/http://www.example.com
返回的HTTP响应
www.pv.com/http://www.example.com是完整的表示
http://www.example.com.我需要能够在此响应中添加自己的Javascript/HTML文件.
查看/sf/answers/2289325321/,唯一的区别是它使用不同的目标参数:
Run Code Online (Sandbox Code Playgroud)var http = require('http'); var httpProxy = require('http-proxy'); var proxy = httpProxy.createProxyServer({}); http.createServer(function(req, res) { proxy.web(req, res, { target: 'http://www.google.com' }); }).listen(3000);
这可以解释Invalid host错误:您需要传递主机作为target参数,而不是整个URL.因此,以下可能有效:
options = {
ignorePath: true,
changeOrigin: false
}
var proxy = httpProxy.createProxyServer({options});
router.get(function(req, res) {
var url = req.body.url;
proxy.web(req, res, { target: url.protocol + '//' + url.host });
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1748 次 |
| 最近记录: |