Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs: routing table using http-proxy

Trying to put in place an http proxy with a custom routing logic using http-proxy 1.4.3, following this tuto and executing the code below:

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);

When trying, as shown in the tuto, to access http://localhost:8080/anything I end up with the following error

/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)

as I'm a nodejs noobie, I'm really clueless.

like image 413
MaK Avatar asked Sep 17 '14 17:09

MaK


1 Answers

So as mentioned by @BadZen in the comments above, the code was outdated, after going through the http-proxy readme, i've end up editing my code to this (and yeah it works), of course i'm creating apache vhosts using proxy-reverse for foo.loc and bar.loc so they point to localhost:9000, but it's out of the scope of this question

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);
like image 82
MaK Avatar answered Oct 10 '22 04:10

MaK