Search code examples
node.jsexpressnode.js-connect

Routing to different node.js express-apps on one server


I want to run different apps on one server, but the routing between them should be made with paths and not with sub-domains.

I read about bouncy and the connect vhost middleware, but they just allow me to route sub-domains to different ports.

I want something like this:

domain.com -> app1

domain.com/api -> app2, in order that /api is / for app2

domain.com/some/path -> app3, in order that /some/path is / for app3

The position in the route tree should be transparent to the apps and it would be nice if I don't have to restart any other app when adding a new one.

Do I have to code it myself or is there some solution out there?


Solution

  • Assuming each app is running in its own process, you essentially need a reverse proxy.

    http-proxy is your best bet. You can mix an Express app and the proxy ("app1"), and forward requests for /api to app2 and /some/path to app3.

    app2 and app3 can be running on the same box or different boxes.


    If this is all happening in the same process, just use the router from app1 and app2 mounted to a path:

    var app1=express(), app2=express(), app3=express();
    
    app1.use(app1.router);
    app1.use('/api', app2.router);
    app1.use('/some/path', app3.router);