Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why node server has to be restart on file change?

Tags:

node.js

server

Why is it necessary to restart a NodeJS server when there is a file change? Do other servers like Apache, IIS, nginx require this as well or can they restart automatically?

like image 864
rajagopalx Avatar asked Dec 15 '15 05:12

rajagopalx


1 Answers

You don't say which files you're talking about so I'll mention the issues with a couple different types of files.

For Javascript files that make up your Javascript code for your node.js server, node.js is a continuously running server. That means when the server starts up, it parses your Javascript code into memory and then starts executing it. That server process stays running until you stop it. Because node.js is a continuously running server, if you want to update the Javascript files that make up the server code, you have to stop the server and restart it to let it load and reparse the newly changed source files.

This is very different than something like PHP with Apache that runs a given PHP script from scratch for each separate request. Since there is no long running PHP application and each PHP script is started from scratch for each request, then it can automatically pick up a newly changed PHP script without restarting the Apache server. If you had a long running server written entirely in PHP, then it would likely have similar behavior as node.js.

And, if you wanted to upgrade your Apache server code, you'd have to restart Apache (as with node.js).

You can kind of think of node.js as Apache + PHP in one since the functions of both are generally met by just node.js by itself. It integrates the web server functionality with the webapp logic whereas those are separate with Apache + PHP.


For HTML files or Javascript files that are served by the server and delivered to the browser, you will generally not have to restart the server for the new versions of those files to be served on subsequent browser requests. But, this depends a bit on which server framework you are using and how exactly it implements file caching. This behavior is not specific to node.js, but would be built into whatever code you were using to serve files by your web server (e.g. Express or something like that).

like image 180
jfriend00 Avatar answered Oct 06 '22 07:10

jfriend00