Home > Mobile >  Node js server running multiple or on one script via nginx
Node js server running multiple or on one script via nginx

Time:01-28

I am relatively new to node js servers.

I have an Nginx server with multiple node js scripts running (Reverse Proxy). Since all the scripts are very different currently I am running multiple node js scripts.

eg

https://page.com/stuff1 -> stuff1.js (node js server running on port 81)

https://page.com/stuff2 -> stuff2.js (node js server running on port 80)

I like how the code in each file is simpler and how if I wanted to I can turn off one page without affecting the other.

My question is if it is better to concentrate all the node js scripts into one or if I should stick to what I am doing now.

I have done some research and the questions I come across seem to be about jquery and plug-ins and not the server itself. They also seem to focus on sending the files to the client.

Thank you for reading.

CodePudding user response:

Your question is valid, and this scenario may serve in some situations :)

About this part:

My question is if it is better to concentrate all the node js scripts into one or if I should stick to what I am doing now.

Might be more complex to answer, as it really depends on your use case, and where and how you want to use your applications.

  • In some cases, it's just better to write a clean, and maintainable code using some design and architectural patterns like the "clean architecture", or the "hexagonal architecture". And with this, you may have your application running behind one Node.js server, in one code base but it can scale and serve multiple users while keeping the code easy to maintain and debug. And if you want to turn off a page, you can just "unregister" the routes from your Node application.
  • In other cases, your approach might be better, so you would split your application into smaller applications and put them behind a reverse proxy, this allows you to maintain them separately which might be beneficial, especially if you would work in a bigger team. Also, now you may have the applications running on the same server, it's much easier to move them to different servers, for more scalability and better fault tolerance, and you only do the necessary changes on your Nginx configuration and possibly your server's firewall configs.

And of course, both approaches have cons: You may not want to handle a big code base if the application is too complex, and you don't want to scale a whole application just to serve more traffic on coming to an isolated module of your application.

On the other hand, you may not want to deal with the issues that come with making your application distributed. You would have (in this case) two applications sharing resources on the same server which may cause challenges when you scale.


Edit:

You may want to have a look at pm2's process management as it should help you manage multiple Node.js processes.

  • Related