- synchronous:- one command at a time.
- single-threaded :- in a specific synchronous ouder
- a function that is passed to another function as an argument.
- Node.js is a javaScript runtime built on chrome's V8 javaScript engine.
- Node.js is a javaScript running on the server.
- In 2009 an idea came in the mind of Ryan Dahi that why not run javaScript outside browser, so he took V8 engine and embedded in a c++ program and called it Node.exe later on become Node.js .
- I/o Bound
- Data Streaming Applications
- Real time chat Applications
-
The REPL feature of Node is very useful in experimenting with Node.js codes and to debug javaScript codes.
-
The Node. js Read-Eval-Print-Loop (REPL) is an interactive shell that processes Node. js expressions. The shell reads JavaScript code the user enters, evaluates the result of interpreting the line of code, prints the result to the user, and loops until the user signals to quit
-
Read - Reads user's input, parses the input into javaScript data-structure, and stores in memory.
-
Eval - Takes and evaluates the data Structure.
-
print - prints the result.
-
Loops - Loops the above commend until the user presses ctrl-c twice.
.break Sometimes you get stuck, this gets you out
.clear Alias for .break
.editor Enter editor mode
.exit Exit the REPL
.help Print this help message
.load Load JS from a file into the REPL session
.save Save all evaluated commands in this REPL session to
a file
Press Ctrl+C to abort current expression, Ctrl+D to exit the
REPL
type nul >home.js
- JS Expression
- use veriables
- Multiline code / loops
- use (_) to get the last result
- we cand use editor mode
- function call kari kari value pass kale argument kahe san
- function defind kala bele gin value expact kar su seta k paramentr kahe san
-
consider modules to be the same as javaScript libraies.A set of functions you want to include in your application
-
Node.js has a set of buit-in modules which you can use without any further installation.
const fs = require("fs");
fs.writeFileSync("notes.txt", "Hello i am learning node");
fs.appendFileSync("notes.txt", " This is the second line");
- Node.js uincludes an additional data type called Buffer.
- (NOt avilable in browser's javaScript).
- Buffer is mainly used to store binary data,
- while reading form a file or feceiving packets over the notework.
const buf_data = fs.readFileSync("notes.txt");
console.log(buf_data.toString());
Hello i am learning node This is the second line This is the
second line This is the second line
fs.renameSync("notes.txt", "readwrite.txt");
-
- Create a folder named it NodeCh
-
- Create a file in it named bio.txt and data into it.
-
- Add more data into the file at the end of the existing data.
-
- Read the data without getting the buffer data at first. (file encoding)
-
- Rename the file name to mybio.txt
-
- now delete both the file and the folder.
- i.
const fs = require("fs");
fs.mkdirSync("Nodech");
- ii.
fs.writeFileSync("bio.txt", "My Name is Om Parakash");
- iii.
fs.appendFileSync("bio.txt", " I am a software engineer");
- iv.
const data = fs.readFileSync("bio.txt", "utf8");
console.log(data);
- My Name is Om Parakash I am a software engineer
- v.
fs.renameSync("bio.txt", "myBio.txt"); // rename file
- vi.
fs.unlinkSync("myBio.txt"); // delete file
fs.rmdirSync("NodeLearn"); // delete directory
- a charactor encoding capable of encoding all possible characters (Called code piont)in Unicode.
- code unit is 8-bits.
- use one to four code units to encode Unicode.
const fs = require("fs");
fs.writeFile("test.txt", "Today is a Awesome day", (err) => {
if (err) {
console.log(err);
} else {
console.log("File created");
}
});
// callback function:- a function that is passed to another function as an argument
- We pass them a function as an argument - a callback - that gets called when that gets called when that task complets.
- The callback has an argument that fells yu whether the operation completed sucessfully.
- Now we need to say what to do when fs.writeFile has completed (even if it's nothing ), and start checking for errors.
fs.appendFile("test.txt", " Have a Nice day", (err) => {
if (err) {
console.log(err);
} else {
console.log("File created");
}
});
fs.readFile("test.txt", "utf8", (err, data) => {
if (err) {
console.log(err);
} else {
console.log(data);
}
});
O/p:-
- Today is a Awesome day Have a Nice day
-
- Create a folder named it NodeCh
-
- Create a file in it named bio.txt and data into it.
-
- Add more data into the file at the end of the existing data.
-
- Read the data without getting the buffer data at first. (file encoding)
-
- Rename the file name to mybio.txt
-
- now delete both the file and the folder.
- i.
fs.mkdir("NodeCh", function (err) {
if (err) {
console.log(err);
} else {
console.log("folder created");
}
});
- ii.
const fs = require("fs");
fs.writeFile("bio.txt", "Have a nice day", (err) => {
if (err) throw err;
console.log("File created");
});
- iii.
const fs = require("fs");
fs.appendFile(
"bio.txt",
"๐ If you find this repo helpful then do not forget to give a star โ๏ธ to this repository.",
(err) => {
if (err) throw err;
console.log("File created and data written to file");
}
);
- iv.
const fs = require("fs");
fs.readFile("bio.txt", "utf-8", (err, data) => {
if (err) {
console.log(err);
} else {
console.log(data);
}
});
O/p:-
-
Have a nice day๏ฟฝ If you find this repo helpful then do not f orget to give a star โ๏ธ to this repository.
-
v.
const fs = require("fs");
fs.rename("bio.txt", "mybio.txt", (err) => {
if (err) throw err;
console.log("File Renamed");
});
- vi.
const fs = require("fs");
fs.unlink("mybio.txt", (err) => {
if (err) throw err;
console.log("File deleted!");
});
// delete file mybio.txt
Node.JS OS Module To Get Operating System Info
ctrl + shift + ecs
const os = require("os");
console.log(os.platform()); // khow the operating system is running
const freeMemory = os.freemem();
console.log(`Free memory: ${freeMemory / 1024 / 1024 / 1024} GB`);
const totalMemory = os.totalmem();
console.log(`Total memory: ${totalMemory / 1024 / 1024 / 1024} GB`);
console.log(os.arch());
console.log(os.type());
console.log(os.tmpdir());
x64
Windows_NT
Total memory: 7.826770782470703 GB
Free memory: 2.9341506958007812 GB
win32
C:\Users\91907\AppData\Local\Temp
๐ PATH Module In Node.JS
-The node:path module provides utilities for working with file and directory paths. It can be accessed using:
const path = require('path');
- path module is use for geting the path of the file or directory in the system for creating the path of the file or directory in the system and also for getting the path of the file or directory.
console.log(path.dirname("/Users/jagrut/Desktop/NodeJS/PahModule/Path.js"));
console.log(path.extname("/Users/jagrut/Desktop/NodeJS/PahModule/Path.js"));
console.log(path.basename("/Users/jagrut/Desktop/NodeJS/PahModule/Path.js"));
console.log(path.parse("/Users/jagrut/Desktop/NodeJS/PahModule/Path.js"));
// parse method is use for getting the parsed object of the file or directory
console.log(path.parse("/Users/jagrut/Desktop/NodeJS/PahModule/Path.js").name);
/Users/jagrut/Desktop/NodeJS/PahModule
.js
Path.js
{
root: '/',
dir: '/Users/jagrut/Desktop/NodeJS/PahModule',
base: 'Path.js',
ext: '.js',
name: 'Path'
}
Path
index.js
const add = require("./operator");
console.log(add(5, 5));
operator.js
const add = (a, b) => a + b;
module.exports = add;
// index.js
const { add, sub, mul } = require("./operator");
console.log(add(5, 10));
console.log(sub(50, 10));
console.log(mul(50, 10));
// operator.js
const add = (a, b) => a + b;
const sub = (a, b) => a - b;
const mul = (a, b) => a * b;
module.exports = { add, sub, mul };
// export multiple modules
- o/p:-
15
40
500
- npm is a package manager for the javaScript programming language it is the difault package manager for the javaScript runtime enverment Node.js.
npm i chalk@4.1.2
npm i validator@13.1.1
- if the email is true the turn green else red.
- nodemon is a tool that helps develop Node.js based applications by automatically restarting the node application when file changes in the directory are detected.
npm install -g nodemon
or using yarn: yarn global add nodemon
// module wrapper function
(function (exports, require, module, __filename, __dirname) {
// module code
const name = "Om ";
console.log(name + "is a good person");
})();
- module wrapper function is a function that is used to wrap the code of a module.
- MODULE FUNCTION is a private function .it is not accessible outside the module.
- module wrapper function is used to make the code of a module more scalable.
console.log(__filename);
console.log(__dirname);
console.log(module);
D:\Node.js_Learn\ModwrapperFun\index.js
D:\Node.js_Learn\ModwrapperFun
Module {
id: '.',
path: 'D:\\Node.js_Learn\\ModwrapperFun',
exports: {},
filename: 'D:\\Node.js_Learn\\ModwrapperFun\\index.js',
paths: [
'D:\\Node.js_Learn\\ModwrapperFun\\node_modules',
'D:\\Node.js_Learn\\node_modules',
'D:\\node_modules'
]
}
- To access web pages of any webapplication, you need to a web server.
- The web server will handle all the http requests for the web application.
- e.g IIs is a web server for ASP.NET web applications and Apache is a web server for PHP of java web applications.
- Node.js provides capabilities to create your own web server which will hendle HTTP requests asynchronously. you can use IIS or Apache to run Node.js web application but it is recommendent to use node.js web server.
// create own web server
const http = require("http");
const server = http.createServer((req, res) => {
res.end("Hello World\n");
});
server.listen(3000, () => {
console.log("Server is listening on port 3000");
});
- The http.createServer() method creates a new web server.
- The http.createServer() method includes request and response parameters which is supplied by the Node.js.
- The http.createServer() method takes a callback function as an argument.
- The callback function is called whenever a request is made to the server.
- The request object object can be used to get information about the current http request.
- eg.:- request.url, request.method, request.headers, request.body
- The server is a Node.js object.
- It can be used to create a web server that can listen on a port and respond to HTTP requests.
const http = require("http");
const server = http.createServer((req, res) => {
// console.log(req.url);
if (req.url === "/") {
res.end("Hello from Heme\n");
} else if (req.url === "/about") {
res.end("Hello from about\n");
} else {
res.writeHead(404, { "Content-Type": "text/html" });
res.end(" <h1> 404 Not Found\n <h1/> ");
}
});
server.listen(3000, () => {
console.log("Server is listening on port 3000");
});
- JSON :- JavaScript object Notation.
- JSON is a lightweight
- format for storing and transporting data.
- JSON is often used when data is sand form a server to a web page.
const bioData = {
name: "Om Prakash",
age: "19",
hobbies: ["reading", "coding"],
};
// stringify use for convert object to json
const convJSON = JSON.stringify(bioData);
console.log(convJSON);
const bioData = {
name: "Om Prakash",
age: "19",
hobbies: ["reading", "coding"],
};
// parse use for convert json to object
const convObject = JSON.parse(convJSON);
console.log(convObject);
{"name":"Om Prakash","age":"19","hobbies":["reading","coding"]}
{ name: 'Om Prakash', age: '19', hobbies: [ 'reading', 'coding' ] }
- Convert to JSON
- dusre file me add kardena
- readfile
- again convert back to json obj to original
- console.log
- 1
const bioData = {
name: "Om Prakash",
age: "19",
hobbies: ["reading", "coding"],
};
// convert to json
const jsonData = JSON.stringify(bioData);
console.log(jsonData);
- 2
const fs = require("fs");
const bioData = {
name: "Om Prakash",
age: "19",
hobbies: ["reading", "coding"],
};
// convert to json
const jsonData = JSON.stringify(bioData);
console.log(jsonData);
// use fs in json file wirte
fs.writeFile("json1.json", jsonData, (err) => {
// write file
if (err) {
console.log(err);
} else {
console.log("File written");
}
});
- 3
const fs = require("fs");
const bioData = {
name: "Om Prakash",
age: "19",
hobbies: ["reading", "coding"],
};
// convert to json
const jsonData = JSON.stringify(bioData);
console.log(jsonData);
fs.readFile("json1.json", "utf8", (err, data) => {
if (err) {
console.log(err);
} else {
console.log(data);
}
});
- 4
const fs = require("fs");
const bioData = {
name: "Om Prakash",
age: "19",
hobbies: ["reading", "coding"],
};
// convert to json
const jsonData = JSON.stringify(bioData);
console.log(jsonData);pattjoshi
// convert to original data
const data = JSON.parse(jsonData);
console.log(data);
- API is the acronym for Application Pprogramming interfacem which is a sortware interface that allows tow applications to talk to each other.
- Each time you use an application like Facebool, send on instant message, or check the weather on your phone, you're using an API.
const http = require("http");
const fs = require("fs");
const server = http.createServer((req, res) => {
const data = fs.readFileSync(`${__dirname}/userApi.json`, "utf-8");
// console.log(data);
const oject = JSON.parse(data);
// console.log(req.url);
if (req.url === "/") {
res.end("Hello from Heme\n");
} else if (req.url === "/about") {
res.end("Hello from about\n");
} else if (req.url === "/userapi") {
// res.end(oject[0].email);
res.writeHead(200, { "Content-Type": "application/json" });
res.end(data);
} else {
res.writeHead(404, { "Content-Type": "text/html" });
res.end(" <h1> 404 Not Found\n <h1/> ");
}
});
server.listen(3000, () => {
console.log("Server is listening on port 3000");
});
// console.log(__dirname);
- Node.js has a built-in module, called "Events"
- Where you can create, fire, and listen for-your own events.
- creating a class for the event mod to inherit from the event emitter class in node js to create an event mod that can be used in the game to create events.
// Events for the Event Mod
const EventEmitter = require("events");
const event = new EventEmitter();
// creating a new event emitter class to create a new event mod.
- streams are a way to handle data as it is being received.
- streams are objects that can be used to read and write data from a source to a destination in continuous fashion. in Node.js, there are four streams:
- streaming means listening to music or watching a video in 'real time', instade of downloading the entire file and then playing it.
- readable streams - we can read data from a stream
- writable streams - we can write data to a stream
- duplex streams - we can read and write data to a stream
- transform streams - A type of duplex stream which the output is computed based on the input.
-
Instance and throws severalevents at diffetent instance of times.
-
For example, some of the commonly used events are:
-
data :- when data is available to read
-
end :- when the is no more data to read
-
error :- when there is an error reading or writing data
-
finish :- when all the data has been flushed to the underlying system.
const fs = require("fs");
const http = require("http");
const server = http.createServer();
server.on("request", (req, res) => {
const rstream = fs.createReadStream("input.txt");
rstream.on("data", (chunk) => {
res.write(chunk);
});
rstream.on("end", () => {
res.end();
});
});
server.listen(3000, () => {
console.log("server is listening on port 3000");
});
- the method used to take a credable stream and connect it to a writeable steam.
- pipe() method - pipes data from one stream to another.
- pipe() method is used to pipe data from one stream to another.
const fs = require("fs");
const http = require("http");
const server = http.createServer();
server.on("request", (req, res) => {
const rstream = fs.createReadStream("input.txt");
rstream.pipe(res);
});
server.listen(3000, () => {
console.log("server is listening on port 3000");
});