【问题标题】:Error while trying to re-direct application to new server port尝试将应用程序重定向到新服务器端口时出错
【发布时间】:2015-06-24 04:41:53
【问题描述】:

我正在创建新服务器来监听新端口(下面是第二个创建),现在当我使用某个端口调用应用程序时,我想将其重定向到新创建的服务器端口,并将消息放入浏览器中“请求路由到 9009"

我使用以下代码创建服务器

httpProxy = require('http-proxy');

proxy = httpProxy.createProxyServer({});

    http.createServer(function (req, res) {
            var hostname = req.headers.host.split(":")[0];
             if  (hostname ==='localhost') {
              proxy.web(req, res, {target: 'http://localhost:9009'});
             }     
}    }).listen(3000, function () {
        console.log('App listening on port 3000');
    });
now I create the new server

http.createServer(function (req, res) {
 res.writeHead(302, {
    'Location': 'http://localhost:9009'
 });

   res.end("Request route to  9009");
}).listen(9009);

现在当我输入 localhost:3000 它重定向我到 localhost:9009(这 正是我需要的我可以在浏览器中看到)但我得到了 错误 This webpage has a redirect loop ERR_TOO_MANY_REDIRECTS

如果我从 second createServer 函数中删除以下内容

res.writeHead(302, {
    'Location': 'http://localhost:9009'
 }); 

它不是重定向,我没有收到错误... 我把这段代码放在错误的地方了吗?或者有办法让它不同吗? 我用https://github.com/nodejitsu/node-http-proxy

更新

我把代码改成如下

var http = require('http');

http.createServer(function (req, res) {
    console.log("Server created");
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.write('9009 here' + '\n' + JSON.stringify(req.headers, true, 2));
    res.end();
}).listen(9009);

http.createServer(function(req, res) {
    console.log("Server 2 created");
    res.writeHead(302, {
        'Location': 'http://localhost:9009/'
    });
    res.end("Request route to 9009");
}).listen( 3001 );

【问题讨论】:

  • 如果您希望客户端重定向到 9009,为什么要将端口 9009 代理到端口 3000?当您代理时,您使内容在端口 9009 上可用,在端口 3000 上也可用。如果您只是想将流量从端口 3000 发送到另一个端口,请不要使用代理,只需 http 重定向它们。
  • 您希望每个访问者都被重定向到一个新的唯一端口,还是所有访问者都被重定向到端口 9009?
  • @dandavis-让现在假设应用程序将路由到 9009 是否可能?你能帮忙怎么做吗?

标签: javascript node.js url-redirection http-proxy node-http-proxy


【解决方案1】:

在您显示的代码中,代理使端口 9009 显示在端口 3000 上,更像是 apache url 重写而不是 url 重定向。

如果您想将登陆端口 3000 的访问者发送到端口 9009,简单的 http 就足够了:

var http = require('http');

http.createServer(function (req, res) {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.write('9009 here' + '\n' + JSON.stringify(req.headers, true, 2));
  res.end();
}).listen( 9009 );

http.createServer(function(req, res) {
    res.writeHead(302, {
        'Location': 'http://X.X.X.X:9009/' // fix me
     });
    res.end("Request route to  9009");
}).listen( 3000 );

如果您希望每个访问者都访问一个新的个人端口,这是一种简单但幼稚的方法(不考虑重复端口,这将使节点崩溃 1/1000 次):

var http = require('http');
http.createServer(function(req, res) {
    var port=Math.floor(Math.random()*1000)+12000;
    http.createServer(function (req, res) {
      res.writeHead(200, { 'Content-Type': 'text/plain' });
      res.write( port + ' talking' + '\n' + JSON.stringify(req.headers, true, 2));
      res.end();
    }).listen( port );

    res.writeHead(302, {
        'Location': 'http://X.X.X.X:'+ port +'/'  // fix me
     });
    res.end("Request route to " + port );
}).listen( 3000 );

【讨论】:

  • 非常感谢这个工作,投票赞成!我还有两个问题 1. 当我在 localhost:3000 上的浏览​​器中点击时,我在控制台中看到 Server 2 created Server created Server created 这 3 个条目,当我点击 9009 时,我看到两次服务器创建,可以吗 3和2个电话? 2. 如果我想添加一些逻辑来处理帖子中的 URL 并在您建议的地方拨打电话?
  • 1.我不确定你在问什么。第一个代码应该为十个客户端创建两台服务器,第二个代码应该为十个客户端创建十一台服务器。我不会使用第二个代码,除非您编写更多代码来处理在客户端完成时关闭服务器。 2.我认为POST有重定向问题,所以你想把那个代码放在9009端口的服务器中。
猜你喜欢
  • 2012-02-29
  • 2014-10-09
  • 2014-03-23
  • 2014-07-18
  • 1970-01-01
  • 2011-06-16
  • 1970-01-01
  • 1970-01-01
  • 2016-04-03
相关资源
最近更新 更多