Preview:
demofile1.html
<html>
<body>
<h1>My Header</h1>
<p>My paragraph.
</p> </body>
</html>
Create a Node.js file that reads the HTML file, and return the content:
Example
var http = require('http'); var fs = require('fs'); http.createServer(function (req, res) { fs.readFile('demofile1.html', function(err, data) { res.writeHead(200, {'Content-Type': 'text/html'}); res.write(data);

return res.end(); });


}).listen(8080);


OUTPUT:


Create Files:
The File System module has methods for creating new files: fs.appendFile()
fs.open() fs.writeFile()

fs.appendFile() :
The fs.appendFile() method appends specified content to a file. If the file does not exist, the file will be created:
Example:
Create a new file using the appendFile() method:
var fs = require('fs');
fs.appendFile('mynewfile1.txt', 'Hello content!', function (err) { if (err) throw err;
console.log('Saved!');
});


OUTPUT:






fs.open() :
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:
Example:
Create a new, empty file using the open() method: var fs = require('fs');
fs.open('mynewfile2.txt', 'w', function (err, file) { if (err) throw err;
console.log('Saved!');
});

OUTPUT:




fs.writeFile() :
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:
Example:
Create a new file using the writeFile() method: var fs = require('fs');
fs.writeFile('mynewfile3.txt', 'Hello content!', function (err) { if (err) throw err;
console.log('Saved!');
});
OUTPUT:

Update Files:
The File System module has methods for updating files:

fs.appendFile() fs.writeFile() fs.appendFile():
The fs.appendFile() method appends the specified content at the end of the specified file:
Example:
Append "This is my text." to the end of the file "mynewfile1.txt": var fs = require('fs');
fs.appendFile('mynewfile1.txt', ' This is my text.', function (err) { if (err) throw err;
console.log('Updated!');
});


OUTPUT:





fs.writeFile() :
The fs.writeFile() method replaces the specified file and content:
Example:
Replace the content of the file "mynewfile3.txt": var fs = require('fs');
fs.writeFile('mynewfile3.txt', 'This is my text', function (err) { if (err) throw err;
console.log('Replaced!');
});
OUTPUT:




Delete Files:
To delete a file with the File System module, use the fs.unlink() method. The fs.unlink() method deletes the specified file:
Example:
Delete "mynewfile2.txt":
var fs = require('fs'); fs.unlink('mynewfile2.txt', function (err) { if (err) throw err;
console.log('File deleted!');
});
OUTPUT:
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter