Running multiple Node (Express) applications on the same port

I have several Node applications (build on Express).

Now I put them like this:

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

Now I want to run these 3 applications on the same port (say, 8080). Is it possible?

It should be noted that each application has such common routes as these -

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

I basically want to do this, as you can do with installing Apache / PHP.

So, when you have the LAMP stack,

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

You can easily access them in the form of different applications -

  • localhost/app1
  • localhost/app2
  • localhost/app3
+42
javascript express
Jun 27 '12 at 12:16
source share
3 answers

You can use app.use() :

 app .use('/app1', require('./app1/index').app) .use('/app2', require('./app2/index').app) .listen(8080); 
+43
Jun 27 '12 at 14:44
source share

You can run them as separate applications, listening to different ports, and then have a proxy (for example, https://github.com/nodejitsu/node-http-proxy/ ) serving everything on 8080 depending on the requested URL.

as:

 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 a charm for me ( http://nerdpress.org/2012/04/20/hosting-multiple-express-node-js-apps-on-port-80/ ). I was not so keen on installing them as sub-applications, as suggested in the comments, because I wanted them to run independently ...

+22
Jun 27 '12 at 12:30
source share

You can create one main application (for example, an application) in parallel with your applications and initialize secondary applications (in your case app1, app2, app3) using app.use ('', require ('./app1/yourApp.js') .

All your applications (app1, app2, app3) should create an application and export it using var app = module.exports = express (); You do not need to create a server instance or call app.listen in all subapps; all sub-applications can be served through the main application listening port.

0
May 28 '17 at 2:26 p.m.
source share



All Articles