Instructions for the fs.readFile method in node.js

  • 2020-05-05 10:55:08
  • OfStack

method description:

The contents of the file are read asynchronously.

Without the top content encoding, the output is in buffer format, such as < Buffer 32 33 31 32 33 31 32 33 31 32 33 >

syntax:


fs.readFile(filename, [encoding], [callback(err,data)])

Since this method belongs to the fs module, the fs module (var fs= require(" fs "))

needs to be introduced before use

receive parameters:

filename       file path

options           option object, containing encoding, encoding format, this item is optional.

callback           callback, passing 2 parameter exceptions err and file contents data

Example of :


var fs = require('fs');
fs.readFile('content.txt','utf-8', function(err,data){
 if(err){
  console.log(err);
 }else{
  console.log(data);
 }
})


Related articles: