60

I have multiple Node applications (build on Express framework).

Now I have placed them like this -

  • /var/www/app1
  • /var/www/app2
  • /var/www/app3

Now I want to run these 3 apps on the same port (say 8080). Is that possible ?

One thing to note is that, Each app has common routes like these -

  • app.get('/', func...);
  • app.get('/about', func...);
  • app.post('/foo', func...);
  • app.post('/bar', func...);

Basically I want to do it like you can do with Apache/PHP setup.

So with a LAMP stack when you have -

  • /var/www/app1
  • /var/www/app2
  • /var/www/app3

You can easily access them as different apps from -

  • localhost/app1
  • localhost/app2
  • localhost/app3
Gajus
  • 69,002
  • 70
  • 275
  • 438
user1437328
  • 15,546
  • 9
  • 34
  • 44
  • 1
    possible duplicate of [How to mount express.js sub-apps?](http://stackoverflow.com/questions/8514106/how-to-mount-express-js-sub-apps) – lanzz Jun 27 '12 at 12:22

3 Answers3

66

You can use app.use():

app
  .use('/app1', require('./app1/index').app)
  .use('/app2', require('./app2/index').app)
  .listen(8080);
Gajus
  • 69,002
  • 70
  • 275
  • 438
user1437328
  • 15,546
  • 9
  • 34
  • 44
36

You could run them as seperate apps listening to different ports and then have a proxy (like https://github.com/nodejitsu/node-http-proxy/ ) serving everything on 8080 depending on the requested URL.

like:

var options = {
  router: {
    'foo.com/baz': '127.0.0.1:8001',
    'foo.com/buz': '127.0.0.1:8002',
    'bar.com/buz': '127.0.0.1:8003'
  }
};

Works like charm for me ( http://nerdpress.org/2012/04/20/hosting-multiple-express-node-js-apps-on-port-80/). I wasn't so keen on having them mounted as sub-apps, as suggested in the comments because i wanted them to run independently...

gherkins
  • 14,603
  • 6
  • 44
  • 70
  • 1
    First of all, i don't want to setup multiple vhosts. i want it like vhost/app1 , vhost/app2 , vhost/app3. secondly, i tried the second method from the blog post you linked to, but it's not really working for me and i don't know whether it'll fit my use case or not. any idea ? oh, and also not looking to use a reverse proxy at this point... – user1437328 Jun 27 '12 at 13:53
  • Of course you can run your subapps wrapped in one app and handle the routing internally. I personally just don't like my apps depend on each other that much, when it comes to crashes. Thats why i prefer running them on different ports. – gherkins Jun 27 '12 at 14:03
  • you don't have to set up multiple vhost to achieve this with node-http-proxy , i'll update the answer with an example – gherkins Jun 27 '12 at 14:04
  • could you clarify how to use the "options" var? – A.D Jun 29 '16 at 19:50
  • apparently the `router` in options was removed a while ago. https://blog.nodejitsu.com/node-http-proxy-1dot0/ – galki Oct 09 '17 at 13:26
0

You can create one main app(say app) parallel to you apps, and have it initializing the secondary apps (in your case app1, app2, app3) using

app.use('<the_context_you_need>', require('./app1/yourApp.js')

All your apps (app1, app2, app3) need to create app and export it by using

var app = module.exports = express();

You need not create instance of server or call app.listen in all the subapps; all the sub-apps can be served via main app listen port.

Sanjay Manohar
  • 6,920
  • 3
  • 35
  • 58