Node JS
❖ REPL - It is a feature of Node Js which can be used for experimenting Node Js
code and debug the javascript code
➢ R - Read : Read user’s input, parses the input into JS data structure, and
store in memory
➢ E-Eval : Takes and evaluates the data structure
➢ P-Print : Print the result
➢ L-Loop : Loops the above command until the user process ctrl+c twice
❖ 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());
❖ Node Js CRUD Operations in Synchronous file system with eg:
➢ C-Create - Here we create a folder named it as dir or something
■ Create a file inside it named as file.txt and data into it
■ Eg: const fs =require(‘fs’);
fs.mkdirSync(‘dir’); -Create a folder
fs.writeFileSync(‘dir/file.txt’,’Welcome to Node’);-Create a file and
data inside it
➢ 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);
➢ D-Delete - Delete the file and folder using fs method.
■ fs.unlinkSync(‘dir/newfile.txt);
■ To delete folder : fs.rmdirSync(‘dir’);
❖ Node js CRUD Operation in Asynchronous file system with eg:
➢ Create - fs.writeFile(‘file.txt’,’Welcome’,(error)=>{
console.log(“My file is created”);
console.log(error);
});
■ Here a call back function is required. Inside that we can either pass
the argument or not. No problem here just passed error argument if
any error comes while creating file , error argument will be
activated,( ithu koduthillelum no problem.)
➢ 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’,()=>{
});
❖ Difference b/w Synchronous and Asynchronous system in Node Js
➢ In synchronous it takes one execution at a time. After completion of one
execution it goes to next
➢ But in asynchronous it wont wait for the execution time. All task will be
executed at a time and whichever completed first will be printed first and
then the other and other etc
Features of Express js
❖ Faster server side development
❖ Middleware -checking all the conditions
❖ Routing
❖ Templating and debugging
HTTP Methods
❖