Node Js Tutorial
- Node JS Introduction
- Node JS Installation
- Node JS Architecture
- Node JS Modules
- Node Js: FILE SYSTEM MODULES
- EVENTS IN NODE JS
- Node JS and Mysql
- Create Application in Node JS
- Global Object in Node JS
- Creating and loading Node Module
- EXTEND EVENT EMITTER
Questions & Answers
Node Js: FILE SYSTEM MODULES
FS modules are like read a file, write a file, rename the file, append a file etc.
As when you work on project you want some own modules or third party modules. As in building projects different files are needed and the working with files the FS modules made it easy.
As just take a files app.js Take a topic like reading a file so in this read file function is used and different parameters are used like name of file “cal.js”, encoder/decoder function i.e. utf8 and call back function to check if reading file process is completed or not. In Call back function first parameter is “err” and second parameter is “data”. So one once you read the file that data will be stored in this “data” variable. Now let’s talk how to print data for this-:
“Console.log (data)” script is used.
So here by writing Read file code the data of calc.js file is shown in output
Now see how to write a file:
In write a file- fs.write file function is used in which another three parameters are used like “name of file”, “console.log ()” and function with parameter ‘err’.
Now if you want to append-:
Then instead of writing write a file, replace it with “append a file”, so here in calc.js file data will be added in the end and that what append means.
Now let’s see deleting of data, what will be happen to the file by applying deleting function for it. For deletion- “unlink ()” function is used. The Parameters used in this is name of the file, call back function
After run this code calc1.js file will be deleted and after running you don’t find calc1.js file.