在 Node 中代理请求 [英] Proxying requests in Node
问题描述
我需要能够提供副本网站(到 www.google.com、www.facebook.com 等任何站点)通过我的节点服务器.我找到了这个库:
https://github.com/nodejitsu/node-http-proxy
我在代理请求时使用了以下代码:
options = {忽略路径:真,更改来源:假}var proxy = httpProxy.createProxyServer({options});router.get(function(req, res) {proxy.web(req, res, { target: req.body.url });});
但是,此配置会导致大多数站点出错.根据站点的不同,我会收到来自目标 url 的 Unknown service
错误,或者 Invalid host
......类似这些内容.但是,当我通过
changeOrigin: true
我得到了一个正常运行的代理服务,但我的用户浏览器被重定向到他们请求的实际 url,而不是我的(所以如果 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 文件.
看https://stackoverflow.com/a/32704647/1587329,唯一的区别是它使用了不同的目标参数:
<块引用>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' });}).听(3000);
这可以解释 Invalid host
错误:您需要将主机作为 target
参数传递,而不是整个 URL.因此,以下可能有效:
options = {忽略路径:真,更改来源:假}var proxy = httpProxy.createProxyServer({options});router.get(function(req, res) {var url = req.body.url;proxy.web(req, res, { target: url.protocol + '//' + url.host });});
I need to be able to offer replica sites (to www.google.com, www.facebook.com, etc. any site) through my node server. I found this library:
https://github.com/nodejitsu/node-http-proxy
And I used the following code when proxying requests:
options = {
ignorePath: true,
changeOrigin: false
}
var proxy = httpProxy.createProxyServer({options});
router.get(function(req, res) {
proxy.web(req, res, { target: req.body.url });
});
However, this configuration causes an error for most sites. Depending on the site, I'll get an Unknown service
error coming from the target url, or an Invalid host
... something along those lines. However, when I pass
changeOrigin: true
I get a functioning proxy service, but my the user's browser gets redirected to the actual url of their request, not to mine (so if req.body.url = http://www.google.com
, the request will go to http://www.google.com
)
How can I make it so my site's url gets shown, but so that I can exactly copy whatever is being displayed? I need to be able to add a few JS files to the request, which I'm doing using another library.
For clarification, here is a summary of the problem:
The user requests a resource that has a
url
propertyThis
url
is in the form ofhttp://www.example.com
My server, running on
www.pv.com
, need to be able to direct the user towww.pv.com/http://www.example.com
The HTTP response returned alongside
www.pv.com/http://www.example.com
is a full representation ofhttp://www.example.com
. I need to be able to add my own Javascript/HTML files in this response as well.
Looking at https://stackoverflow.com/a/32704647/1587329, the only difference is that it uses a different target parameter:
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);
This would explain the Invalid host
error: you need to pass a host as the target
parameter, not the whole URL. Thus, the following might work:
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 });
});
For the URL object, see the NodeJS website.
这篇关于在 Node 中代理请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!