I’m trying to get a node.js script to display the contents of a text file if the contents of the file changes.
Here’s what I’ve got so far:
var http = require('http');
var fs = require("fs");
http.createServer(function (req, res) {
fs.watch("/Users/{username}/Desktop/data.txt",function(event,file) {
fs.readFile('/Users/{username}/Desktop/data.txt', 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(data);
//^ this doesn't work, just returns a blank page
});
});
}).listen(1337, '0.0.0.0');
Unfortunately, it only displays a blank page. What am I doing wrong…?
How about this: