Skip to content

Instantly share code, notes, and snippets.

@billywhizz
Created December 3, 2010 10:21
Show Gist options
  • Save billywhizz/726797 to your computer and use it in GitHub Desktop.
Save billywhizz/726797 to your computer and use it in GitHub Desktop.
example of hot code loading in a web server
var http = require('http');
var Script = process.binding('evals').Script;
var httpd = new http.Server();
httpd.on("listening", function() {
console.log('Server running at http://0.0.0.0:8124/');
});
var context = {
"name": "billy"
};
var sc = new Script("context.foo = function(req, res) {res.writeHead(200, {'Content-Type': 'text/plain'});res.end('Hello ' + context.name);}");
sc.runInNewContext({"context": context});
httpd.on("request", context.foo);
httpd.listen(8124, "0.0.0.0");
setTimeout(function() {
console.log("reload");
httpd.removeListener("request", context.foo);
var sc = new Script("context.foo = function(req, res) {res.writeHead(200, {'Content-Type': 'text/plain'});res.end('Goodbye ' + context.name);}");
sc.runInNewContext({"context": context});
httpd.on("request", context.foo);
}, 10000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment