Enroll in the training to get access to this lesson along with hundreds of other lessons and complete courses.
Already enrolled? Login
File System
In this lesson, we are going to look at some new concepts
- Module
- Read Write Files
- Callback
- Aysnc
We’ll look at how modules work in Node.js, then we’re going to use a module to read & write files on the system. As you know, JavaScript on the browser does not have access to the file system but with the help of Node.js runtime, JavaScript has access to your file system.
In the process, we’re going to see how callbacks work in Node.js and how asynchronous code is written in Node.js as well.
TIP:
One small tip that I want to give to you guys is that if you have a long list of items in your terminal or command like your machine name, the location name and the username along with the $ as shown below.
You can get rid of that if you want to, but it’s just personal preference. To accomplish that you can write export PS1=”\w$ ”
the forward slash and w represents the folder location
Now I’m going to create a folder and call it filesystem. To create a folder you can write mkdir filesys and your file will be created. I named my folder filesys but you can call it whatever you like. Write cd filesys
to go inside the folder and there I’ll create a file inside the folder and open it in sublime text (text editor). To create a file, simply write touch code.js
You can name filename whatever you want. So let’s start writing code. The first thing I’m going to do is to import the module from the Node.js core installation. One of the module node.js has is called fs or file system.
A module is a piece of functionality that node.js may have it as part of their entire coordbase. You can also create module to supplement some kind of information. You can create a module as well on like how to pick a date picker, how to calculate dates and export it to the node package manager that we’re going to look soon.
There’s a module which is pre bundled with the node insulation is called file system. It allows you to access the file system of your computer.
To import any module, I can create a variable and call it fs like such
var fs = require('fs')
The fs module is now accessible in the program. To read a file I'm going to write fs.readFile('');
it takes a few parameters. The first one is the file that we want to read. I'm going to create another file, and you know how to do it. Yes! you guessed it right, we're going to write touch
and the file name. You can see below the file is now created.
Let's write something inside that file "Hello World, and Hello Node Programmers" just for demonstration. The second parameter it takes is a function, and the function further takes two parameters. One is the error written as err
and the second one is data
or you can call it anything. I'm going to console.log( )
for example. Also I'll change the text file in the editor to text2.txt
just for demonstration purposes.
var fs = require('fs')
fs.readFile('text.txt', function(err, data){
if(err){
console.log(err);
}
console.log(data);
});
If I run the code in the terminal by writing node code.js. You will see an error that the file path does not exist and the reason is that because we didn't enter the exact file name. Undefined is the data, it did not read any data.
This time I'll change the file name from "text2.txt"
to "text.txt
" which is the correct file name.
var fs = require('fs')
fs.readFile('text.txt', function(err, data){
if(err){
console.log(err);
}
console.log(data);
});
Save it, go to the terminal and write code node.js again. This time you will notice that the buffer information got printed. Buffer is a data type in Node.js that is an information representing binary code.
Since we cannot understand the binary code, we understand it in coding format called utf8
. You can write it in the code as shown below.
Now run the same code again, and you will see the text got printed.
How to write a file
The next thing we are going to learn is how to write a file. So this time I'm going to write fs.writeFile
and this time I'll create a new file for demonstration and you can see in the image below that code.js file is inside the filesys
and to go up one directory, you can write ../
and pass a few parameters like add 'some text'
the 'uft8'
and the callback function
fs.writeFile('../text2.txt', 'some text', 'utf8'
, function(err, data){
if(err){ console.log(err) }
console.log(data);
})
Also the second part got printed first in the console and the first part got processed later.
Check out the previous lesson to learn more about the Asynchronous and Event Loop. But let's say if you want a certain part to run first, there's a method in file system called readFilesync
. Also the synchronous method does not need the callback function. So I'll remove the callback function from the code. I'll create a variable and save the value that we want to print like such
var myRead = fs.readFileSync('text.txt', 'utf8');
console.log(myRead);
var myWrite = fs.writeFileSync('../text2.text', 'some text', 'utf8');
console.log(myWrite);
Save it, run the code one more time by writing node code.js and you will notice the first part got processed first and the second one got processed later
Get Started
Already have an account? Please Login