小编gre*_*ish的帖子

使用node-http-proxy转发每个请求

我正在尝试设置一个只转发请求的node-http-proxy.最后,这个代理应该在我通过浏览器访问的每个网站中注入javascript.

现在,大多数页面都被正确转发和显示,但有些页面,例如posterkoenig.chverkehrsclub.ch正在返回空白页面或页面上有错误.两个站点都可以在没有代理的情况下正常运行 我需要更改什么,或者我错过了哪些内容未正确转发?

我是nodejs的新手,甚至不确定我的方法是否有效.

这是我到目前为止所得到的:

var httpProxy = require('http-proxy');
var url = require('url');

httpProxy.createServer(function(req, res, proxy) {

  var urlObj = url.parse(req.url);

  proxy.proxyRequest(req, res, {
    host: urlObj.host,
    port: 80,
    changeOrigin: true,
    enable : { xforward: true }
  });
}).listen(9000, function () {
  console.log("Waiting for requests...");
});
Run Code Online (Sandbox Code Playgroud)

更新

正如@robertklep建议我删除changeOrigin和重新定义req.headers.host,也req.headers.url

posterkoenig.ch:

现在抛出:

An error has occurred: 
{"code":"ENOTFOUND","errno":"ENOTFOUND","syscall":"getaddrinfo"}
Run Code Online (Sandbox Code Playgroud)

verkehrsclub.ch:

首页现在可以使用,但是子页面仍会在页面上抛出错误.

var httpProxy = require('http-proxy');
var url = require('url');

httpProxy.createServer(function(req, res, proxy) {

  var …
Run Code Online (Sandbox Code Playgroud)

javascript http-proxy node.js node-http-proxy

12
推荐指数
1
解决办法
1万
查看次数

标签 统计

http-proxy ×1

javascript ×1

node-http-proxy ×1

node.js ×1