Reboot the working node.js server

Let's say I have a nodejs / express application on a production server. If I want to add a new router, I have to reboot it, right? But if I restart my node.js server, users may receive an error message.

So, how can I restart my node.js server without errors for my users?

+4
source share
2 answers

If you proxy your Node.js application using Nginx, you can tell your Node application to listen on the socket, and then only close the old server if the new one starts correctly; as soon as the old server shuts down, Nginx will forward requests to the new server instance. Here's how Unicorn , the popular Ruby server, works.

In Nginx, you should specify your upstream as follows:

upstream node_server { server unix:/path/to/socket.sock; } server { ... location / { ... proxy_pass http://node_server; } } 

And in node you can listen on socket with

 server.listen('/path/to/socket.sock', function() { console.log("Listening on socket."); }); 
+9
source

I assume that the answer will check everything on an intermediate server (i.e. an exact copy of the code you plan to deploy next to the existing production code) and choose a time with low traffic and just switch the directory on top to a new one.

If there is a problem, return it.

But the actual "downtime" will be just a second, and if no request is sent at the moment, they will not receive an error.

However, I have never tried this myself, and I'm curious to know what the best answer is! Good question!

0
source

All Articles