【问题标题】:How do I do a 302 redirection with node-http-proxy?如何使用 node-http-proxy 进行 302 重定向?
【发布时间】:2014-10-11 19:15:50
【问题描述】:

我的代码:

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

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

【问题讨论】:

    标签: node.js http http-proxy node-http-proxy


    【解决方案1】:

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

    1. 包括跟随重定向包(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, });

    【讨论】:

      猜你喜欢
      • 2013-03-25
      • 2015-11-20
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 2020-02-11
      相关资源
      最近更新 更多