Hot Code Loading in Node.js

05 Feb 2010 | By Alex Young | Tags nodejs server

In Hot Code Loading in Node.js Blaine Cook describes how to dynamically reload code in node.js projects. He’s created a branch of node.js called hotload that provides the feature.

Hot code loading uses sandboxes and caching to reload code when it changes. If you had a web app and updated some files, it would automatically reload the code for new requests.

His example looks like this:

var requestHandler = require('./myRequestHandler');

process.watchFile('./myRequestHandler', function () {
  module.unCacheModule('./myRequestHandler');
  requestHandler = require('./myRequestHandler');
}

var reqHandlerClosure = function (req, res) {
  requestHandler.handle(req, res);
}

http.createServer(reqHandlerClosure).listen(8000);

Strictly speaking it’s not as advanced as the way Erlang behaves, but it could ease deployment and avoid abrupt process reloading.

blog comments powered by Disqus