Example file system in nodejs

The Node.js file system module allow you to work with the file system on your computer.

To include the File System module, use the require() method:

 

var fs = require('fs');

 

Example1: read files

Create file index.html

 

<html>

<body>

<h1>Welcome my page</h1>

<p>Hello world !</p>

</body>

</html>

 

Create file readFile.js

 

var http = require('http');

var fs = require('fs');

http.createServer(function (req, res) {

  // read file and show via http

  fs.readFile('index.html', function(err, data) {

    res.writeHead(200, {'Content-Type': 'text/html'});

    res.write(data);

    res.end();

  });

}).listen(8080);

 

Run program via cmd:

 

C:\Nodejs> node readFile.js

 

Go browser http://localhost:8080/

 

Welcome my page

Hello world !

 

Example 2:  create new file one of the ways

Code create a file createFile.js

The fs.appendFile() method appends specified content to a file. If the file does not exist, the file will be created:

 

var fs = require('fs');

fs.appendFile('newFile.txt', 'Hello content!', function (err) {

    if (err) {

        console.log(err);

    } else {

        console.log('Saved!');

    }

 });

 

The fs.open() method takes a "flag" as the second argument, if the flag is "w" for "writing", the specified file is opened for writing. If the file does not exist, an empty file is created:

 

var fs = require('fs');

fs.open('newfile1.txt', 'w', function (err, file) {

    if (err) {

        console.log(err);

    } else {

        console.log('Saved!');

    }

});

 

The fs.writeFile() method replaces the specified file and content if it exists. If the file does not exist, a new file, containing the specified content, will be created:

 

var fs = require('fs');

fs.writeFile('newFile2.txt', 'Hello content!', function (err) {

    if (err) {

        console.log(err);

    } else {

        console.log('Saved!');

    }

});

 

Run program via cmd:

 

C:\Nodejs>node readFile.js

 

File created in folder C:\Nodejs\

Example 3: Update file

Create file updateFile.js

 

var fs = require('fs');

//fs.appendFile() method appends the specified content at the end of the specified file

fs.appendFile('newFile.txt', ' This is my text.', function (err) {

  if (err) throw err;

  console.log('Updated!');

});

 

Or

 

var fs = require('fs');

//Replace the file with a new one:

fs.writeFile('newFile.txt', 'This is my text.', function (err) {

  if (err) throw err;

  console.log('Replaced!');

});

 

Run program via cmd:

 

C:\Nodejs>node updateFile.js

 

File updated in folder C:\Nodejs\

Example 4: Delete file

 

var fs = require('fs');

//Delete the file newFile.txt:

fs.unlink('newFile.txt', function (err) {

  if (err) throw err;

  console.log('File deleted!');

});

 

Run program via cmd:

 

C:\Nodejs>node deleteFile.js

 

File deleted in folder C:\Nodejs\

Exampl 5 :  rename file

 

var fs = require('fs');

fs.rename('newFile.txt', 'file_Renew.txt', function (err) {

  if (err) throw err;

  console.log('File Renamed!');

});

 

Run program via cmd:

 

C:\Nodejs>node renameFile.js

File rename in folder C:\Nodejs\