1

trying to read a file using node.js

    var fs = require('fs');
    fs.readFile("details.txt",'utf8',function(err,data)
    {
        if(err)
        {
            return console.log(err);
        }
        response.write(data);
    });

there is a file alredy with some text in the same directory.

Error im getting is :

response.write(data); ^ ReferenceError: data is not defined

6
  • Have you tried using "./details.txt" as the path? Commented Apr 17, 2016 at 14:07
  • @mcgraphix — Why should that make a difference? Commented Apr 17, 2016 at 14:09
  • 1
    I can't think of any reason why err would be false but data undefined. Commented Apr 17, 2016 at 14:10
  • ./ refers to the working directory. Juat trying to ensure it is looking in the right place. Commented Apr 17, 2016 at 14:15
  • yes. still giving the same error. Commented Apr 17, 2016 at 14:21

3 Answers 3

1

File order

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();
  }
});
Sign up to request clarification or add additional context in comments.

3 Comments

Yes!!! It's worked. Thanks so much!!!! Any tips of retrieving the output line by line ? at the moment it is displaying all the details in one line.
@CJM You can use readLine function instead. stackoverflow.com/a/32599033/3359432
Ok, I have edited the code for write data to. data.split('\n').join('<br>')
0
   var fs = require('fs');
    fs.readFile("details.txt",'utf8',function(err,data)
    {
        if(err)
        {
            return console.log(err);
        }
        console.log(data);
    });

the error is

response.write(data)

Comments

0

Try using the full path to 'details.txt' (i.e. '/etc/hosts'). And, send it to console.log just to confirm it works.

2 Comments

it is in the home directory where all the other files are server.js, router.js
I can write into the file but only when im reading it gives me that error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.