【问题标题】:nodejs: routing table using http-proxynodejs:使用 http-proxy 的路由表
【发布时间】:2014-09-17 17:16:10
【问题描述】:

尝试使用http-proxy 1.4.3 设置具有自定义路由逻辑的http 代理,遵循tuto 并执行以下代码:

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

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

    var hostname = req.headers.host.split(":")[0];
    var pathname = url.parse(req.url).pathname;

    // Options for the outgoing proxy request.
    var options = { host: hostname };

    // Routing logic
    if(hostname == "127.0.0.1") {
        options.port = 8083;
    } else if(pathname == "/upload") {
        options.port = 8082;
        options.path = "/"; 
    } else {
        options.port = 8081;
    }
    // (add more conditional blocks here)

    proxy.proxyRequest(req, res, options);

}).listen(8080);

console.log("Proxy listening on port 8080");

// We simulate the 3 target applications
var http = require("http");

http.createServer(function(req, res) {
    res.end("Request received on 8081");
}).listen(8081);

http.createServer(function(req, res) {
    res.end("Request received on 8082");
}).listen(8082);

http.createServer(function(req, res) {
    res.end("Request received on 8083");
}).listen(8083);

当尝试访问 http://localhost:8080/anything 时,如教程中所示,我最终遇到以下错误

/var/www/node.proxy/node_modules/http-proxy/lib/http-proxy/index.js:119
throw err;
      ^
Error: Must provide a proper URL as target
    at ProxyServer.<anonymous> (/var/www/node.proxy/node_modules/http-proxy/lib/http-proxy/index.js:68:35)
    at Server.closure (/var/www/node.proxy/node_modules/http-proxy/lib/http-proxy/index.js:125:43)
    at Server.EventEmitter.emit (events.js:98:17)
    at HTTPParser.parser.onIncoming (http.js:2108:12)
    at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:121:23)
    at Socket.socket.ondata (http.js:1966:22)
    at TCP.onread (net.js:525:27)

因为我是一个 nodejs 菜鸟,我真的一无所知。

【问题讨论】:

  • 请发布您的代码。
  • 我使用的代码与上面的教程链接完全相同
  • 链接中断。粘贴的代码不会
  • @BadZen 所以是的,代码已经过时了,我使用新的 http-proxy 包语法在代码下方发布

标签: node.js node-http-proxy


【解决方案1】:

正如@BadZen 在上面的 cmets 中所提到的,代码已经过时了,在通过 http-proxy 自述文件之后,我最终将我的代码编辑到这个(是的,它可以工作),我当然是为foo.locbar.loc 使用proxy-reverse 创建apache vhost,因此它们指向localhost:9000,但这超出了这个问题的范围

var http = require('http'),
    httpProxy = require('http-proxy'),
    proxy = httpProxy.createProxyServer({}),
    url = require('url');

http.createServer(function(req, res) {
    var hostname = req.headers.host.split(":")[0];
    var pathname = url.parse(req.url).pathname;

    console.log(hostname);
    console.log(pathname);

    switch(hostname)
    {
        case 'foo.loc':
            proxy.web(req, res, { target: 'http://localhost:9001' });
            break;
        case 'bar.loc':
            proxy.web(req, res, { target: 'http://localhost:9002' });
            break;
        default:
            proxy.web(req, res, { target: 'http://localhost:9003' });
    }
}).listen(9000, function() {
    console.log('proxy listening on port 9000');
});

// We simulate the 3 target applications
http.createServer(function(req, res) {
    res.end("Request received on 9001");
}).listen(9001);

http.createServer(function(req, res) {
    res.end("Request received on 9002");
}).listen(9002);

http.createServer(function(req, res) {
    res.end("Request received on 9003");
}).listen(9003);

【讨论】:

  • 我正在尝试此代码,但有:错误:套接字挂起并且代理完全崩溃。我怎样才能解决这个问题 ?我有带有网络套接字的 http/https 请求
猜你喜欢
  • 2011-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多