DailyJS

Hot Code Loading in Node.js

Alex R. Young

Subscribe

@dailyjs

Facebook

Google+

server nodejs

Hot Code Loading in Node.js

Posted by Alex R. Young on .
Featured

server nodejs

Hot Code Loading in Node.js

Posted by Alex R. Young on .

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.