Reading a file with Node.js

Updated: Mar 8



Read file in asynchronously


Read content of a file in a non-blocking is called asynchronous way. That is, to tell Node to read in the file, and then to get a callback when the file-reading has been finished.


Callbacks


Callbacks are a basic idiom in Node.js for asynchronous operations. When most people talk about callbacks, they mean the function that is passed as the last parameter to an asynchronous function. The callback is then later called with any return value or error message that the function produced.


Code for write and read file

const fs = require("fs");

// this is for write file

 fs.writeFile("read.txt","today is  monday", (err) => {
     console.log(" file is created")
  });

// Read file asynchronously

    const fs = require("fs");
    fs.readFile("read.txt","UTF-8",(err, data) =>{

 console.log(data);
    });

Tree structure of files






Run code

PS C:\node js> cd .\fsAsync\
PS C:\node js\fsAsync> node index.js

Output

today is monday

Contact us for this nodejs assignment Solutions by Codersarts Specialist who can help you mentor and guide for such nodejs assignments.


If you have project or assignment files, You can send at contact@codersarts.com directly, or contact


Contact Us

Tel: (+91) 0120  4118730  

Time :   10 : 00  AM -  08 : 00 PM IST 

Registered address: G-69, Sector 63, 

 Noida - 201301, India

We Provide Services Across The different countries

USA    Australia   Canada   UK    UAE    Singapore   New Zealand    Malasia   India   Ireland   Germany

Terms and Conditions        Privacy Policy    Pricing Policy       Refund Policy         COPYRIGHT © 2021

Sofstack Technology Solutions Pvt. Ltd.

  • CodersArts | Linkedin
  • Instagram