
Node JS
- Node JS Installer
- Node JS Documentation
npm initcommand is used to initial a package.json file.node <filename>.jscommand is used to run a javascript file using NodeJS- To use NodeJS in web browser, in
index.htmladd<script src=""></script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
/>
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
</head>
<body>
<script src="./index.js"></script>
</body>
</html>Node Modules
- File Based
- Build In
- Third Party
1. File Based
index.js file
module.exports = {a, b}is used to export the selected modules, for re-use of code.
// console.log("Hello World")
const a = 100;
const b = {
average: (a, b) => {
console.log((a + b) / 2);
},
percent: (a, b) => {
console.log(a / b / 100);
},
};
module.exports = { a, b };app.js file
require()is a built-in function to include external modules that exist in separate files
const { a, b } = require("./index.js");
console.log(a);
b.average(10, 20);
b.percent(10, 20);2. Build In
fs(File System Module) allows us to work with files present locally in the system.pathmodule allows us to work with directories and file paths.os(Operation System module) provides information about the computer's operating system.eventshelps trigger an action / event on a computer
Reading Files
- reading value from
sample.txtfile
Hello NodeJS !Asynchronous Functions
- The heavy tasks are worked in background meanwhile, rest of the code is executed once
- heavy task is executed the Call-Back function is called Non-Blocking Nature ✅
asynchronouse.jsfile
const fs = require("fs");
// console.log(fs);
fs.readFile("./sample.txt", "utf-8", (error, data) => {
if (error) {
throw error;
}
console.log(data);
});
console.log("I am First");- Output
I am First
Hello NodeJS !
Process finished with exit code 0console.log("I am First")is executed first becausefs.readFile()is Asynchronous function
Synchronous Function
- Runs file line by line, waits of complete execution current line complete
- Makes JavaScript Code Slow and is called Blocking Nature ❌
synchronous.jsfile
const fs = require("fs");
const a = fs.readFileSync("./sample.txt", "utf-8");
console.log(a);
console.log("I am first");- Output
Hello NodeJS !
I am first
Process finished with exit code 0Writing Files
Asynchronous Function
asynchronous.jsfile
const fs = require("fs");
const a = "This is output";
fs.writeFile("./sample.txt", a, () => {
console.log("Written");
});
console.log("I am First");- Output
I am First
Written
Process finished with exit code 0sample.txtfile
This is outputSynchronous Function
synchronous.jsfile
const fs = require("fs");
const a = "This is output";
fs.writeFileSync("./sample.txt", a);
console.log("Written");
console.log("I am First");- Output
Written
I am First
Process finished with exit code 0sample.txtfile
This is output3. Third Party
- Third Party modules are modules which are written by some else, and are used in a project.
- Examples: Express, Gulp, React, Mongoose, Async
Using pokemon module
- installing
pokemonmodule by usingnpm install pokemon
╭─ npm install pokemon
added 3 packages, and audited 5 packages in 5s
2 packages are looking for funding
run `npm fund` for detailsindex.jsfile
const pokemon = require("pokemon");
console.log(pokemon.all());- Output
[
'Bulbasaur', 'Ivysaur', 'Venusaur', 'Charmander', 'Charmeleon',
'Charizard', 'Squirtle', 'Wartortle', 'Blastoise', 'Caterpie',
'Metapod', 'Butterfree', 'Weedle', 'Kakuna', 'Beedrill',
'Pidgey', 'Pidgeotto', 'Pidgeot', 'Rattata', 'Raticate',
'Spearow', 'Fearow', 'Ekans', 'Arbok', 'Pikachu',
'Raichu', 'Sandshrew', 'Sandslash', 'Nidoran♀', 'Nidorina',
... 865 more items
]
Process finished with exit code 0Using nodemon module
nodemonmodule is a simple monitor script for use during development of a Node.js app- installing
nodemonglobally, bynpm install nodemon -g
╭─ npm install nodemon -g
added 32 packages in 2s
3 packages are looking for funding
run `npm fund` for detailsCreating a NodeJS server
index.jsfile
const http = require("http");
const PORT = 4000;
const HOST = "localhost";
const server = http.createServer((request, response) => {
response.end("Working...");
});
server.listen(PORT, HOST, () => {
console.log(`Server is working on http://${HOST}:${PORT}`);
});- Terminal
Server is working on http://localhost:4000- Localhost : 4000

Routing using NodeJS Server
index.htmlfile
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
/>
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<style>
html {
background-color: #1e2537;
color: white;
height: 100vh;
font-family: Arial;
}
</style>
</head>
<body>
<div className="home">
<h1>Home Section</h1>
</div>
</body>
</html>route.jsfile
const http = require("http");
const fs = require("fs");
const PORT = 4000;
const HOST = "localhost";
const home = fs.readFileSync("./index.html", "utf-8");
const server = http.createServer((request, response) => {
if (request.url === "/") {
response.end(home);
} else if (request.url === "/about") {
response.end("<h1 style='font-family: Arial'>About Page</h1>");
} else {
response.end(
"<h1 style='font-family: Arial; color: #FC6E68'>404 Page Not Found</h1>"
);
}
});
server.listen(PORT, HOST, () => {
console.log(`Server is working on http://${HOST}:${PORT}`);
});- LocalHost: 4000

Deploying the NodeJS server
npm startcommand to work, add"start": node ./server/route.jsin"scripts" : {}object inpackage.jsonfile
{
"name": "00-basics",
"version": "1.0.0",
"description": "learning nodeJS",
"main": "modules/file-based/index.js",
"scripts": {
"start": "node ./server/index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"prettier": "2.8.7"
},
"dependencies": {
"pokemon": "^2.3.4"
}
}- Output
╭─ npm start
> 00-basics@1.0.0 start
> node ./server/index.js
Server is working on http://localhost:4000