File system module in Node.js provide an API for interacting with the file system on your computer. It allows you to perform various operations such as writing, creating , updating, deleting files and more.
To use fs.module ypu need to require it in your node.js application
Now,let’s explore some common use cases and example of working with file system module
1> Reading a File- You can read the content of using fs.read File method there’s Ex-
Node.js File system is responsible for all asynchronous or synchronous I/O operations.
fs.readFile is responsible for reading physical file asynchronously.
filename- full path and name of a file as string
options- parameter can be object or string which includes encoding is ‘utf-8’ and default flag is “r”.
callback Function -with 2 parameters err and fd. this will get called when read file operation completes.
*2>Writing a File
*
You can write data to a file using fs.writeFile method.
3>append a File- we can appendFile which takes three argument file name,data yo want to append and third callback function which will take parameter err . if there is any error it will throw err if there is no error then it will simply console log the msg.
Conclusion
What Kind of operation we can perform in node.js are as follows-
Importing the module
- Error Handling 2.Asynchronous and synchronous methods 3.File operations- reading, writing, updating, renaming, checking, deleting. 4.file streams
Top comments (0)