I am new to nodejs and callbacks.
So I have this code where I read a file when a request to server is initiated via HTTP:
var http = require("http"); var fs = require("fs"); http.createServer(function(request,response){ response.writeHead(200,{'Content-Type':'text/plain'}); response.end("Server runnning..."); fs.readFile('new.txt',function(err,data){ if(err){ console.error(err); return; } console.log(data.toString()); }); }).listen(1234);
When I run the code, the contents of the file are displayed/logged twice on console.
lorem ipsum lorem ipsum
The file contents are:
lorem ipsum