Last active
January 6, 2016 21:28
-
-
Save thehungrycoder/7e998bdca5135de2b140 to your computer and use it in GitHub Desktop.
Hot reload node module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
"countryA", | |
"countryB" | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)); | |
}) | |
~ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As said on official doc
watchFile
should be replaced withwatch
.