InterviewSolution
| 1. |
Explain the mechanism of reading the content of a file in Node.js? |
|
Answer» Node.js file system module is capable of working on files either on your local system or database, but working on file system we have to call require() method for calling a file server. Through Node.js we can even open any http file and perform read content operation. NodeJs peruses the content of a document in a non-blocking, nonconcurrent way. Node JS utilizes its fs center API to manage DOCUMENTS. The simplest method to peruse the WHOLE SUBSTANCE of a document in nodeJs is with fs.readFile strategy. The following is a test code to peruse a record in NodeJs asynchronously and synchronously. Reading a file in node asynchronous/ non-blocking VAR fs = require('fs'); fs.readFile('DATA', 'utf8', function(err, contents) { console.log(contents); }); console.log('after calling readFile'); Reading a file in node asynchronous/blocking var fs = require('fs'); var contents = fs.readFileSync('DATA', 'utf8'); console.log(contents); |
|