Nuclear Rooster

18Jul/100

Node.JS filesystem watcher: Auto-reload server on code-change

Wow, a 2nd Node.JS post of the day, a Sunday to boot.

I was thinking about how to auto-reload a node server, and then I came across a great solution by accident. DracoBlue has some good Node.JS articles. In particular, check out this one about auto-reloading in addition to a link to the code in their Spludo Node.JS framework which makes this happen.

Simply save the file to your apps top-level directory, change the 'run_server.js' to the name of your file that you want watched, and presto, you're app will be restarted if any js files changes. Love it.

Here's what the console looks like:

steggie:appstat nstielau$ node dev_server.js
DEBUG: DEVSERVER: Starting server
Server running at http://127.0.0.1:8124/ in development
DEBUG: DEVSERVER: Restarting because of changed file at ./appstat_tracker.js
DEBUG: DEVSERVER: Stopping server for restart
DEBUG: DEVSERVER: Child process exited: null
DEBUG: DEVSERVER: Starting server
Server running at http://127.0.0.1:8124/ in development

I wish node had a --watch flag that would do this for me. I took a peek at the node code, but I'm not quite ready to make that happen.

Filed under: Uncategorized No Comments