0

我的代码:

var path = require('path');
var util = require('util');
var http = require('http');
var fs = require('fs');
var httpProxy = require('http-proxy');


httpProxy.createProxyServer({
    target: 'http://localhost:9000',
    agent: https.globalAgent,
    headers: {
        host: 'localhost'
    }
})
.listen(8000);

util.puts('proxy server listening on port 8000');

http.createServer(function targetServer(req, res) {
    res.writeHead(302, { 'Location': '/' }); //Here is the 302
    util.puts('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));

    res.end();

})
.listen(9000);

util.puts('target server listening on port 9000');

当我从浏览器发出请求时,出现以下错误:

Error: socket hang up

我不知道这个错误是什么意思。谁能帮我解决这个问题?

4

1 回答 1

0

我确实让它工作了。
我必须做两件事:

  1. 包括follow-redirects包(npm install follow-redirects)并将我的http初始化更改为:
    var http = require('follow-redirects').http; 来自:var http = require('http');
  2. 在 CreateProxyServer 中包含{changeOrigin: true}属性: var proxy = httpProxy.createProxyServer({changeOrigin: true, toProxy : true, });
于 2015-03-04T19:01:13.233 回答