gloo
gloo

Reputation: 2580

what does fs.readFile send to its callback?

In a nodejs server suppose I read a file like so:

fs.readFile('path/to/file','encoding',function(err,data){
  //send data
  res.end(data);
});

What type of object is data, i.e is it a string, an array or something else?

Upvotes: 0

Views: 872

Answers (2)

Austin Mullins
Austin Mullins

Reputation: 7437

From the docs:

The callback is passed two arguments (err, data), where data is the contents of the file.

If no encoding is specified, then the raw buffer is returned.

The raw buffer is a byte array. You can convert it to a javascript string by calling data.toString(). For more conversion options, see the docs.

Upvotes: 1

YarGnawh
YarGnawh

Reputation: 4664

Without any options set,

http://nodejs.org/api/fs.html#fs_fs_readfile_filename_options_callback

If no encoding is specified, then the raw buffer is returned.

Upvotes: 1

Related Questions