Reading,Updating,Deleting and Renaming Using Node.js
In this tutorial, we are going to learn how to read, update or edit, delete and rename files in Node.js.Previously we learnt how to Use Node.js Http module and creating modules.
Let get Started.
Reading Html File
To read files using node.js we use readFile() function
Save below html code as "demo.html"
<html>
<body>
<h1>Node.js Frameworks</h1>
<ol>
<li>
React.js
</li>
<li>Ember.js</li>
<li>Express.js</li>
</ol>
</body>
</html>
Now let write node.js code to read above html file and display its content in a browser.
Save the code as "readfile.js"
var http=require('http');//import http module
var fs=require('fs')//import files module
var server=http.createServer(function(req,res)//create a server
{
fs=fs.readFile('demo.html',function(error,data)
{
res.writeHead(200,{'Content-type':'text/html'});
res.write(data);
res.end();
});
});
server.listen(8080);//make the server to listen to port 8080
Running the code.
Open Node.js command prompt.
Open the folder containing the two files.My folder is "nprojects"
Run the code by entering "node creatfile.js"
Output:
Reading a txt file
We can also read a .txt file and display content in the console.
Example .
Create a file and name it "file.txt"
Add content to the file and then read it using code.
//Reading file in a Synchronous way
var fs=require('fs');
fs.readFile('file.txt',function(error,data)
{
if(error) throw error;
console.log('Synchronous read:= ' +data.toString());
})
//Reading file Asynchronous way
var content=fs.readFileSync('file.txt','utf8');
console.log('Asybchronous read:= ' +content);
Output.
Updating Files
To update files in Node.js, we can use the following functions.
1.fs.appendFile()-opens a specified file and adds its contents.If the file does not exists its created.
2.fs.open()-this file takes a certain flag.The flag determines if the file is being opened for writing, reading etc.To write to the file we use "w" flag.If the file does not exist its created and if it exists its opened for writing
3.fs.writeFile()-this function creates a file if it does not exists and if it exists content is added to it.
//Function to create file and add content appendFile function
var fs=require('fs');
fs.appendFile('mynewfile.txt',"My fist content",function(error)
{
if(error) throw error;
console.log("File created and saved");
})
//Function to Open file and write to it
fs.open('mynewfile2.txt','w',function(error,file)
{
fs.write(file,'New content added',function(error){
if(error) throw error;
console.log("New content addded");
});})
//WriteFile function.
fs.writeFile('mynewfile3.txt','Writing file number 3',function(error)
{
if(error) throw error;
console.log('File number 3 written')
})
Deleting Files
To delete files in Node.js we use fs.unlink() function.If the file specified file does not exist, an error is thrown
Example
//Deleting files in node.js
var fs=require('fs');
fs.unlink('mynewfile.txt',function(error)
{
if(error) throw error;
console.log('File deleted');
})
Output
Renaming Files using fs.rename()
//Renaming files in node.js
var fs=require('fs');
fs.rename('mynewfile.txt',function(error)
{
if(error) throw error;
console.log('File Renamed');
})