As far as I understand, a Node.js server is basically a JavaScript file, run by the node executable. Is it possible to update this file without stopping request handling?
I guess the node executable must be stopped for this, so I think I should use a reverse proxy. For example:
- Original server version listens on localhost:50001
- Proxy server listens to requests on webinterface:80 and forwards them to localhost:50001
- New server version should be started on localhost:50002
- Proxy server forwarding target should be changed to localhost:50002
- Original server version should be stopped
Is this a valid approach? How can such a version update be done automatically on multiple server machines (accessible from the same LAN)?
A different “pure-node” solution is to use the built-in cluster module:
Your code runs as one cluster-client (of many). A cluster-server process binds to the port and does automatic load-balancing between clients. When you’ve changed the code, you can send a signal to the cluster-server and it will gracefully restart your clients, without dropping any existing connections.
Here are some projects that can do the cluster-server management for you:
Advantages: