Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何使用节点http代理执行302重定向?_Node.js_Http_Http Proxy_Node Http Proxy - Fatal编程技术网

Node.js 如何使用节点http代理执行302重定向?

Node.js 如何使用节点http代理执行302重定向?,node.js,http,http-proxy,node-http-proxy,Node.js,Http,Http Proxy,Node Http Proxy,我的代码: 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

我的代码:

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
我不知道这个错误是什么意思。有人能帮我弄清楚吗?

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

包括跟随重定向程序包npm安装跟随重定向 并更改我的http初始化: 致: var http=require'follow-redirects'。http; 发件人: var http=require'http'; 在CreateProxyServer中包括{changeOrigin:true}属性: var proxy=httpProxy.createProxyServer{changeOrigin:true,toProxy:true,};