2

I'm trying to run 2 instances of NodeJS on the same port and server from diffrent server.js files (diffrent dir, config etc). My server provider gave me an information that vhost is running for a diffrent domain, and there is the question. How to handle it in NodeJS Express app ? I've tried to use vhost from https://github.com/expressjs/vhost like that :

const app = express();
const vhost = require('vhost');

app.use(vhost('example1.org', app));

// Start up the Node server
app.listen(4100, () => {
  console.log(`Node server listening on 4100`);
});

And for second application like that:

const app = express();
const vhost = require('vhost');

app.use(vhost('example2.org', app));

// Start up the Node server
app.listen(4100, () => {
  console.log(`Node server listening on 4100`);
});

But when I'm trying to run second instance I'm getting EADDRINUSE ::: 4100, so vhost doesn't work here.

enter image description here

Do you know how to fix it ?

Patryk Panek
  • 405
  • 4
  • 20
  • 4
    You can't listen on the same port from two different processes. Try using a different port and set the http server so it redirects request to each domain to the corresponding port. What server are you using: Nginx, Apache...? – Miguel Calderón Mar 07 '18 at 08:45
  • It's `LiteSpeed Web Server` https://www.litespeedtech.com/products/litespeed-web-server – Patryk Panek Mar 07 '18 at 08:49
  • I have never used it, but the procedure to set it up to forward requests to a Node app is documented here: https://www.litespeedtech.com/support/wiki/doku.php/litespeed_wiki:other-ext-apps:nodejs-setup – Miguel Calderón Mar 07 '18 at 08:51
  • It's already installed on the server. I just trying to setup and *run* two diffrent application running on diffrent domains example1.org:4100, example2.org:4100. – Patryk Panek Mar 07 '18 at 08:55
  • 1
    Either you serve both domains from the same process or you change one of the ports (which would be my advice). – Miguel Calderón Mar 07 '18 at 09:44
  • 1
    I thought I can make it like I wrote above, but you're right, I created two servers on diffrent ports. – Patryk Panek Mar 07 '18 at 09:51

1 Answers1

6

You can only have one process listen to one port, not just in Node.js, but generally (with exceptions that don't apply here).

You can achieve what you need to one of two ways:

Combine the node apps

You could make the apps into one application, listen once and then forward requests for each host to separate bits of code - if you wanted to achieve code separation still, the separate bits of code could be NPM modules that are actually written and maintained in isolation.

Use webserver to proxy the requests

You could run the 2 node processes on some free port, say 5000 and 5001, and use a webserver to forward requests to it automatically based on host. I'd recommend Nginx for this, as its proxying capabilities are both relatively easy to set up, and powerful. It's also fairly good at not using too many system resources. Apache and others can also be used for this, but my personal preference would be Nginx.

Conclusion

My recommendation would be that you install a webserver and forward requests on the exposed port to the separately running node processes. I'd actually recommend that you run node behind a proxy as default for a project, and only expose it directly in excpetional circumstances. You get a lot of configuration options, security, and scalability benefits if your app already involves a well hardened server setup.

Owen C. Jones
  • 1,435
  • 1
  • 11
  • 13