Skip to content

Instantly share code, notes, and snippets.

@tmcw
Created April 15, 2012 17:32
Show Gist options
  • Save tmcw/2394031 to your computer and use it in GitHub Desktop.
Save tmcw/2394031 to your computer and use it in GitHub Desktop.
minimal live-reload server in node
// A super, super minimal LiveReload implementation in node.
// Relies on polling.
//
// Run as
//
// node reload.js ~/your/project/dir
//
// Add to your HTML like
//
// <script src='http://127.0.0.1:1337/'></script>
var http = require('http'),
fs = require('fs'),
reload;
fs.watch(process.argv[2], function(evt, filename) {
reload = true;
});
http.createServer(function (req, res) {
if (req.url == '/') {
res.writeHead(200, {'Content-Type': 'text/javascript'});
return res.end('(' + ('' + function reload_run() {
window.setInterval(function() {
var im = new Image();
im.onload = function(r) { window.location.reload(); };
im.src = 'http://127.0.0.1:1337/stat?m=' + (+new Date());
}, 1000);
}) + ')()');
}
if (req.url.match(/\/stat/) && reload) {
res.writeHead(200, {
'Content-Type': 'image/gif'
});
reload = false;
return res.end(new Buffer('R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=', 'base64'));
}
return res.end();
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
@tmcw
Copy link
Author

tmcw commented Apr 16, 2012

known issue: this doesn't notice files in nested directories. I'll implement support for them if it isn't much complexity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment