Skip to content

Instantly share code, notes, and snippets.

@thehungrycoder
Last active January 6, 2016 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thehungrycoder/7e998bdca5135de2b140 to your computer and use it in GitHub Desktop.
Save thehungrycoder/7e998bdca5135de2b140 to your computer and use it in GitHub Desktop.
Hot reload node module
[
"countryA",
"countryB"
]
// This file starts a server and listen to port 8989. When you change the module, it'll auto refresh it.
var fs = require('fs'),
http = require('http'),
port = 8989,
file = './country.json',
server,
countries = require(file); // requiring here is important. It didn't work when I set `countries = []`.
fs.watchFile(file, function () {
delete require.cache[require.resolve(file)];
countries = require(file);
})
function handleRequest(req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(countries.join(', '));
}
server = http.createServer(handleRequest);
server.listen(port, function () {
console.log('Server listenting to '.concat(port));
})
~
@rajumsys
Copy link

rajumsys commented Jan 6, 2016

As said on official doc watchFile should be replaced with watch.

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