file read in nodejs
var fs = require('fs'); // blocking, synchronous way var data = fs.readFileSync('/file.txt'); // blocks here until file is read console.log(data); // do something with data // non-blocking, asynchronous way fs.readFile('/file.txt', function (err, data) { if (err) throw err; console.log(data); // do something with data });The fs.readFile() method is used to read files on your computer. There are two ways to use this method: Synchronous: blocks code execution until the file has been read Asynchronous: non-blocking; code continues to run while the file is being read Synchronous example:
var fs = require('fs'); // blocking, synchronous way var data = fs.readFileSync('/file.txt'); // blocks here until file is read console.log(data); // do something with dataAsynchronous example:
var fs = require('fs'); // non-blocking, asynchronous way fs.readFile('/file.txt', function (err, data) { if (err) throw err; console.log(data); // do something with data });