1

I am following http://blog.nodejitsu.com/http-proxy-intro/ to write up my proxy server to point sub domains to node apps running on different port

var http = require('http'),  
httpProxy = require('http-proxy');

//
// Just set up your options...
//
var options = {  
  hostnameOnly: true,
  router: {
    'localhost': '127.0.0.1:80'
    'sub.localhost': '127.0.0.1:9012',
  }
}

//
// ...and then pass them in when you create your proxy.
//
var proxyServer = httpProxy.createServer(options).listen(80)

When i run this file using node and try to access localhost or sub.localhost, I get this error back. I cant really understand whats going wrong.

Error: Must provide a proper URL as target
    at ProxyServer.<anonymous> (D:\myProjects\bitbucket\temp\node_modules\http-proxy\lib\http-proxy\index.js:68:35)
    at Server.closure (D:\myProjects\bitbucket\temp\node_modules\http-proxy\lib\http-proxy\index.js:125:43)
    at emitTwo (events.js:87:13)
    at Server.emit (events.js:172:7)
    at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js:525:12)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:88:23)
Narayan Singh
  • 1,234
  • 2
  • 13
  • 26

1 Answers1

3

Try this:

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

// reverse proxy
var proxy = httpProxy.createProxyServer();
http.createServer(function (req, res) {
    var target,
        domain = req.headers.host,
        host = domain.split(":")[0];
    ////////////////// change this as per your local setup 
    ////////////////// (or create your own more fancy version! you can use regex, wildcards, whatever...)
    if (host === "localhost") target = {host: "localhost", port: "2000"};
    if (host === "testone.localhost") target = {host: "localhost", port: "3000"};
    if (host === "api.testone.localhost") target = {host: "localhost", port: "4000"};
    //////////////////
    proxy.web(req, res, {
        target: target
    });
}).listen(8000);
Neilos
  • 2,696
  • 4
  • 25
  • 51
  • Thank you very much for the answer. Your answer does solve the issue. I would appreciate if you can answer the second part of the question as what does the error means `Error: Must provide a proper URL as target` . – Narayan Singh Mar 09 '16 at 06:30
  • 1
    I do not know exactly what that error means but (at a guess) I suspect that it is because you are not providing a `target` field in your `options` object; the `router` field is ignored as it has been dropped so your proxy never knows where to send requests. – Neilos Mar 10 '16 at 08:40
  • 1
    Essentially in the example I showed we are writing our own router. This is why I suggest that it can be changed/improved, it's really up to you to create your own router logic and as you can see it is not difficult once you know how to wire it up. You could always find a router plugin to do this for you but either way I would not consider the above code to be an adequate production ready solution: all it needs to do is map strings to objects and handle cases where it doesn't match, wildcard/regex support would also benefit you if you have a complex setup. – Neilos Mar 10 '16 at 08:49
  • thanks @Neilos for clearing my doubt about the router target keywords and for giving a better solution. – Narayan Singh Mar 11 '16 at 05:52
  • Working really well on http, but, any way to do the same with an SSL certificate? (through https). – Arnyminer Z Sep 24 '20 at 11:36