CH 4 Node JS
CH 4 Node JS
NODE JS
1. Introduction
Node.js is an open source, cross-platform runtime environment and library that is
used for running web applications outside the client's browser.
Node.js runs the V8 JavaScript engine, the core of Google Chrome, outside of the
browser.
Ryan Dahl developed it in 2009, and its latest iteration, version 15.14, was released
in April 2021.
Developers use Node.js to create server-side web applications, and it is perfect for
data-intensive applications since it uses an asynchronous, event-driven model.
Node.js is used by big business and small enterprises alike. While the likes of
Amazon, Netflix, eBay, Reddit and Paypal all use it, 43% of developers using Node.JS
in 2023 do so for enterprise applications.
2. Features of Node JS
Extremely fast: Node.js is built on Google Chrome's V8 JavaScript Engine, so
its library is very fast in code execution.
I/O is Asynchronous and Event Driven: All APIs of Node.js library are
asynchronous i.e. non-blocking. So a Node.js based server never waits for an
API to return data. The server moves to the next API after calling it and a
notification mechanism of Events of Node.js helps the server to get a
response from the previous API call. It is also a reason that it is very fast.
Single threaded: Node.js follows a single threaded model with event looping.
Highly Scalable: Node.js is highly scalable because event mechanism helps
the server to respond in a non-blocking way.
No buffering: Node.js cuts down the overall processing time while uploading
audio and video files. Node.js applications never buffer any data. These
applications simply output the data in chunks.
Open source: Node.js has an open source community which has produced
many excellent modules to add additional capabilities to Node.js applications.
License: Node.js is released under the MIT license.
3. Installation of node js
You can download the latest version of Node.js installable archive file from
https://nodejs.org/en/
check version: node –v
To execute javascript code outside of browser use node command
C:\AIT>node (enter)
This command starts node environment called REPL
Output:
10
5. REPL
REPL (READ, EVAL, PRINT, LOOP) is a computer environment similar to Shell
(Unix/Linux) and command prompt.
Node comes with the REPL environment when it is installed.
System interacts with the user through outputs of commands/expressions used.
It is useful in writing and debugging the codes.
The work of REPL can be understood from its full form:
Read : It reads the inputs from users and parses it into JavaScript data structure. It is
then stored to memory.
Eval : The parsed JavaScript data structure is evaluated for the results.
Print : The result is printed after the evaluation.
Loop : Loops the input command. To come out of NODE REPL, press ctrl+c twice
Vanita Patil Page 2
6. NPM (Node Package Manager)
It is the default package manager for Node.js and is written entirely in Javascript.
Developed by Isaac Z. Schlueter, it was initially released in January 12, 2010.
NPM manages all the packages and modules for Node.js and consists of command
line client npm.
It gets installed into the system with installation of Node.js.
The required packages and modules in Node project are installed using NPM.
A package contains all the files needed for a module and modules are the JavaScript
libraries that can be included in Node project according to the requirement of the
project.
Installing NPM:
To install NPM, it is required to install Node.js as NPM gets installed with Node.js
automatically.
Checking and updating npm version:
npm –v
3. Uninstall Packages
Use the following command to remove a local package from your project.
C:\>npm uninstall <package name>
The following command will uninstall ExpressJS from the application.
C:\MyNodeProj> npm uninstall express
• 1. Core Modules:
• Node.js has many built-in modules that are part of the platform and come with
Node.js installation. These modules can be loaded into the program by using the
required function.
• Syntax:
• const module = require('module_name');
• The require() function will return a JavaScript type depending on what the particular
module returns. The following example demonstrates how to use the Node.js http
module to create a web server.
• Example:
• const http = require('http');
• http.createServer(function (req, res) {
• res.writeHead(200, { 'Content-Type': 'text/html' });
• res.write('Welcome to this page!');
• res.end();
• }).listen(3000);
• The following list contains some of the important core modules in Node.js:
process provides information and control about the current Node.js process.
querystring utility used for parsing and formatting URL query strings.
• 2. Local Modules:
• Local Modules: Unlike built-in and external modules, local modules are created
locally in your Node.js application. Let’s create a simple calculating module that
calculates various operations. Create a calc.js file that has the following code:
• Filename: calc.js
• exports.add = function (x, y) {
• return x + y;
• };
• exports.sub = function (x, y) {
• return x - y;
• };
• exports.mult = function (x, y) {
• return x * y;
• };
• exports.div = function (x, y) {
• return x / y;
• };
• Since this file provides attributes to the outer world via exports, another file can use
its exported functionality using the require() function.
• Step to run this program: Run the index.js file using the following command:
• node index.js
• Output:
• Addition of 50 and 10 is 60
• Subtraction of 50 and 10 is 40
• Multiplication of 50 and 10 is 500
• Division of 50 and 10 is 5
• 3. Third-party modules:
• Third-party modules are modules that are available online using the Node Package
Manager(NPM).
• These modules can be installed in the project folder or globally. Some of the popular
third-party modules are Mongoose, express, angular, and React.
• Example:
• npm install express
• npm install mongoose
• npm install -g @angular/cli
8. Webserver Creation