I am new to nodejs and callbacks.
So, I have this code where I read the file when a server request 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 / registered twice on the console.
lorem ipsum
lorem ipsum
File contents:
lorem ipsum
source
share