Node Js Notes
Node Js Notes
❖ Core Modules
➢ It is a simple or complex functionality organized in single or multiple JS
files which can be reused throughout the Node js application
➢ Like fs, http, path, util
➢ To access these module write
const varname = require(“core module name”);
Eg : const fs = require(“fs”);
● fs.writeFileSync(‘read.txt’,’Hello Welcome’); -
○ here we create a file called read and content inside
the file is Hello Welcome. This create a file in our
current folder since the folder doesn’t have the file
before. If file was there we could use its name
instead.
● fs.appendFileSync(‘read.txt’, ‘Programing solutions’);
○ Here if we want to add additional data to the existing
file we can give appendFileSync method.
● fs.renameSync(‘read.txt’,’write.txt’);
○ To change the file name we give renameSync method
inside it we pass the arguments as old file name and
new filename.
● const buf_data =
fs.readFileSync(‘write.txt);console.log(buf_data);
○ readFileSync is used to read the data from the file.
But the output shows in the format of buffer
data.consist of many nos and letters like codes.
○ So to read the data inside it properly you have to
convert it to string by using the method called
toString();
○ Eg : console.org(buf_data.toString());
➢ R-Read - Read the data without getting the buffer data at first
■ const readData = fs.readFileSync(‘dir/file.txt’);
console.log(readData.toString());
Or
const readData = fs.readFileSync(‘dir/file.txt’, ‘utf-8’);
console.log(readData);
➢ U-Update - Add more data into the file at the ending of the existing data
■ Also rename the file name to newfile.txt
■ Eg: fs.appendFileSync(‘dir/file.txt’, ‘ Please subscribe’);
■ For rename : fs.renameSync(‘dir/file.txt’,’dir/newfile.txt);
➢ Read - fs.readFile(‘file.txt’,’utf-8,(error,data)=>{
console.log(data);
}); - here error and data arguments are required to call back this
function.it is necessary.
➢ Update - fs.appendFile(‘file.txt’,’ Please help’,()=>{
});
For rename : fs.rename(‘file.txt’, ‘newfile.txt’,()=>{
});
➢ Delete-fs.unlink(‘newfile.txt’,()=>{
});
Features of Express js
❖ Faster server side development
❖ Middleware -checking all the conditions
❖ Routing
❖ Templating and debugging
HTTP Methods
❖