Ricky Mutschlechner
Ricky Mutschlechner

Reputation: 4409

Multiple Node.js "Apps" in one server file bad practice?

At the moment, I'm running two (soon to be three) node.js websocket apps through one server.js file. I'm using express to serve my entire page, as well as a particle physics "game" I wrote. I'm going to be making a chatroom as well, just to learn more about these things.

My issue (or lack thereof?) is that all of this is running through one server.js file. Heroku seems to require this, so I'm not sure if there are any other options. Heroku is what I'm using to host it.

My question is, is this bad practice? Is there something else I should be doing that I'm missing? All of my "apps" are very low-traffic, so I don't think it's a huge issue. I just want to learn the best practices from the get-go so I don't make dumb mistakes. Thanks.

Edit: How do I specifically split the apps up in Heroku, with the procfile setup?

Upvotes: 4

Views: 3352

Answers (1)

Sam Levin
Sam Levin

Reputation: 3416

In my opinion, this is a bad practice if the apps are not related. Running 3 node apps on the same physical or virtual server isn't bad per se, if the resources can handle it. However, running them all through the same Server.js file is going to result in lots of things being where they shouldn't belong. Consider a scenario where a fatal error in app 1 shuts down (or hampers the performance) of app 2 or app 3.

You should encapsulate each app by separating them into separate instances that listen on different ports, and then use a fast reverse proxy (read: nginx) to proxy to them from the web. You can create stateless libraries to encapsulate any shared functionality, and then include those libaries in each app that requires it.

E.g.

app.yourserver.com => localhost:8997 (node app 1)
app2.yourserver.com = localhost:8998 (node app 2)
app3.yourserver.com => localhost:8999 (node app 3)

Here is information on how to set up a reverse-proxy using nginx: http://nginx.com/resources/admin-guide/reverse-proxy

Upvotes: 5

Related Questions