
Above img shows my File order. Note: I'm using IntelliJ IDEA. You will have to find the file "details.txt" in your system.
//Instead of using hard coded path, use: the following code to get the path in Node.js //var path = require('path'); //console.log(path.join(__dirname, '../details.txt')); //require file system var fs = require('fs'); //read file fs.readFile("details.txt",'utf8',function(err,data){ //if error, log error and return if(err) { return console.error('Error: ' + err); } //check if there is data in the file if(data) { //EDITED to include line break //write response //response.write('<div style="color:green">' + data + '</div>'); //write response with <br> response.write('<div style="color:green">' + data.split('\n').join('<br>') + '</div>'); //end the response response.end(); //if no data on the file, do the following }else{ //error variable var error = "Error: file is empty: " + data.length + " bytes."; //log error // console.error(error); //write response response.write('<div style="color:red">' + error + '</div>'); //end response response.end(); } });
errwould be false butdataundefined.