Banner Image

NodeJs for beginners: 03 – File System

December 12, 2021
2 Minutes Read



Banner Image

Looking for other services or solutions?

ux/ui image
Explore our services

In the last blog, we learned about how we can create servers and listen for requests. In this blog we will learn about the the default packages/modules that comes with nodejs.

Nodejs comes with alot of default modules. You can checkout the list of default modules by clicking on this linke: Nodejs Built-in Modules. In this blog we are going to talk about only fs packages:

  • fs (file system)

fs (file system):

If you wants to handle files of your system or you want to send file to a perticular user or you want to perform any kind of operation on files then this modules is used in nodejs. We see some operation on files using node js in this blog. If you want to know all the operation that you could do on files in node js you can check out this link: FS module in node js 

Reading a file in node js:

Create a new file and give it a name. I will create a file with name fs.txt. Inside that file write some random text. Below is an example of fs.txt file.

Image 1: fs.txt

Now in index.js file we will listen for file read request and as a response we will serve this file. Open index.js file and write following code.

Image 2: index.js

In line number 7, we are listen a get request at path “/”.  At line no. 9, we are reading file with readFile() function. readFile() function is used for reading file  asynchronously. If you want to read file synchrounsly then you can use readFileSync() function. After that we are sending that file in response. Now start server with npm start command in the terminal and open you browser and go to http://localhost:3000/. You will see that the content of the fs.txt file is shown to your browser. This is the way node js servers file.

Note* : If you want to use  readFileSync() then you need to change import of fs module to import fs from "fs";

Writing/Updating a file in node js:

Now we will write/update some text in fs.txt file. Write following code in your code-editor:

Variable content contains our new text message. At line number we are writing/updating that text message into fs.txt file. writeFile is asynchronous function. If you want to write synchrounsly then use writeFileSync(). After that we are sending that file to browser. Now if you restart server and go to browser you will see that the file’s content is changed now. But the problem is that the old text of the file is now removed and replaced with the new text. What if we want to not replace the content but append the text in that file. For that you simply need to use appendFile() function instead of writeFile() function.

writeFile() and appendFile() function will create a new file if the file doesn’t exists. Updaing and Writing in node js is same.

Note* : If you want to use  writeFileSync() then you need to change import of fs module to import fs from "fs";


Deleting a file in node js:

Now let’s delete fs.txt file in nodejs. Write following code:

You need to simply use unlink() funciton which will remove your file. Restart your server and go to localhost:3000. Now your file is removed, you can check into your folder if the file is actually removed or not.