Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running multiple Node (Express) apps on same port

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
like image 663
user1437328 Avatar asked Jun 27 '12 12:06

user1437328


People also ask

How do I run multiple node servers?

Yes you can run multiple node instances, all you have to do is to change the port number for which the server is listening. If you are using express framework then you can simply do it like this in app. js file. If you are not using express framework then you have to change in the main file which is generally server.

Can express handle multiple requests?

Express. js use different kinds of middleware functions in order to complete the different requests made by the client for e.g. client can make get, put, post, and delete requests these requests can easily handle by these middleware functions.

Why should you separate express APP and server in NodeJS?

Faster testing execution. Getting wider coverage metrics of the code. Allows deploying the same API under flexible and different network conditions. Better separation of concerns and cleaner code.

How many concurrent connections can NodeJS handle?

To address these issues, Node. JS uses a single thread with an event-loop. In this way, Node can handle 1000s of concurrent connections without any of the traditional detriments associated with threads. There is essentially no memory overhead per-connection, and there is no context switching.


2 Answers

You can use app.use():

app   .use('/app1', require('./app1/index').app)   .use('/app2', require('./app2/index').app)   .listen(8080); 
like image 104
user1437328 Avatar answered Sep 19 '22 01:09

user1437328


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...

like image 44
mgherkins Avatar answered Sep 21 '22 01:09

mgherkins