0% found this document useful (0 votes)
4 views21 pages

Node Js

Node.js is a cross-platform, open-source server-side runtime environment built on Google's V8 JavaScript Engine, designed for executing JavaScript outside a web browser. It features asynchronous, non-blocking APIs, high scalability with a single-threaded model, and does not buffer data, allowing for efficient handling of multiple requests. The document also covers local environment setup, the REPL terminal, command line options, the Node Package Manager (NPM), modules, and the HTTP module for creating servers.

Uploaded by

YS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views21 pages

Node Js

Node.js is a cross-platform, open-source server-side runtime environment built on Google's V8 JavaScript Engine, designed for executing JavaScript outside a web browser. It features asynchronous, non-blocking APIs, high scalability with a single-threaded model, and does not buffer data, allowing for efficient handling of multiple requests. The document also covers local environment setup, the REPL terminal, command line options, the Node Package Manager (NPM), modules, and the HTTP module for creating servers.

Uploaded by

YS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Node Js

➢ [Link] is a server-side runtime environment built on Google Chrome's JavaScript Engine


(V8 Engine).

➢ [Link] was developed by Ryan Dahl in 2009 and its latest version is v20.9.0.

➢ [Link] is a cross-platform (run on Windows, Linux, Unix, macOS, and more), open-source,
back-end JavaScript runtime environment, that executes JavaScript code outside a web
browser.
Features of [Link]
➢ Following are some of the important features that make [Link] the first choice of
software architects.

➢ Asynchronous and Event Driven − All APIs of [Link] library are asynchronous, that is,
non-blocking. It essentially means a [Link] 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 [Link] helps the server to get a response from the previous API call.

➢ Very Fast − Being built on Google Chrome's V8 JavaScript Engine, [Link] library is very
fast in code execution.
Features of [Link]
➢ Single Threaded but Highly Scalable − [Link] uses a single threaded model with event
looping. Event mechanism helps the server to respond in a non-blocking way and makes
the server highly scalable as opposed to traditional servers which create limited threads to
handle requests. [Link] uses a single threaded program and the same program can
provide service to a much larger number of requests than traditional servers like Apache
HTTP Server.

➢ No Buffering − [Link] applications never buffer any data. These applications simply
output the data in chunks.
Local Environment Setup
➢ [Link] can be installed on different OS platforms such as Windows, Linux, Mac OS X, etc
➢ Following are the tools .
▪ The [Link] binary installer.
▪ Node Package Manager (NPM).
▪ IDE or Text Editor.
➢ Binaries for various OS platforms are available on the downloads page of the
official website of [Link].
[Link]
[Link] - REPL Terminal
➢ The [Link] runtime has a built-in interactive shell, in which you can execute
instructions one at a time.
➢ The [Link] interactive REPL terminal is like the Powershell or Command
prompt terminal, or a bash terminal in Linux.
➢ The [Link] interactive shell works on the principle of REPL, which is an
acronym for READ, EVALUATE, PRINT and LOOP.
➢ 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.
➢ Loop − The terminal is ready to receive next input from the user.
[Link] - REPL Terminal
➢ Underscore Variable : You can use underscore (_) to get the last result.
[Link] - REPL Terminal
[Link] Dot Commands & Description
.help
1
shows the dot commands help
.editor
2 enables editor mode, to write multiline JavaScript code with ease. Once you are in this mode, enter ctrl-D to run the
code you wrote.
.break
3 when inputting a multi-line expression, entering the .break command will abort further input. Same as pressing ctrl-
C.
.clear
4
resets the REPL context to an empty object and clears any multi-line expression currently being input.
.load
5
loads a JavaScript file, relative to the current working directory
6 .save
saves all you entered in the REPL session to a file (specify the filename)
7 .exit
exits the repl (same as pressing ctrl-C two times)
[Link] - Command Line Options
➢ :\nodejs> node
➢ :\nodejs> node -i
➢ :\nodejs> node –interactive
➢ :\nodejs> node [Link]
➢ :\nodejs> node –help
➢ :\nodejs> node –v
➢ :\nodejs> node –version
➢ :\nodejs> node -e "[Link](1+2)“
➢ :\nodejs> node --eval "[Link](1+2)"
[Link] - NPM
➢ NPM − an acronym for Node Package Manager, refers to the Command line utility to install
[Link] packages, perform version management and dependency management of [Link]
packages.

➢ npm –v : This command will display version of npm.

➢ Install Package Locally :

Use --save at the end of the install command to add dependency entry into [Link] of
your application.
[Link] - NPM
➢ Install Package Globally : Globally installed packages/dependencies are stored in system
directory. Such dependencies can be used in CLI (Command Line Interface) function of any
[Link] but cannot be imported using require() in Node application directly.

➢ The module will be installed globally, for Windows, the path is


C:\Users\your-username\AppData\Roaming\npm\node_modules.
[Link] - NPM
➢ Update Package :

➢ Uninstall Packages :
[Link] -Module
➢ A module in [Link] is a collection of independent and reusable code that can be
imported into any [Link] application.

➢ A [Link] module is a library of functions, classes and other reusable code, stored in one
or more .js files.

➢ Modules enable a modular and structured approach for developing a [Link] application.

➢ Instead of putting all the functions, classes and methods of an application in a single .js
file, these resources are arranged in separate files (called modules) based on their
relevance.

➢ All the server-side processes are handled by the relevant [Link] module imported into
the application by the require() function.
[Link] -Module
➢ In Node, the `[Link]` is utilized to expose literals, functions, or objects as
modules. This mechanism enables the inclusion of JavaScript files within [Link]
applications. The `module` serves as a reference to the current module, and `exports` is an
object that is made accessible as the module’s public interface.

➢ Syntax:
▪ [Link] = literal | function | object

➢ Import Modules: To include a module, use the require() function with the name of the
module:
➢ Syntax:
▪ var temp = require(‘module_name');
[Link] HTTP Module
➢ [Link] has a built-in module called HTTP, which allows [Link] to transfer data over the
Hyper Text Transfer Protocol (HTTP).
➢ An HTTP server is a computer program or a software component of another program that
acts as a server in a client-server model by implementing the server portion of the HTTP
and/or HTTPS network protocols.
➢ An HTTP server waits for incoming client requests and responds to each one by returning
desired information, including the delivery of the requested web page, or by returning an
HTTP error message.
[Link] HTTP Module
➢ Creating a Basic HTTP Server :
➢ First load the http module which is a core [Link] module.

➢ Create two constants two stores of the value of the hostname and the port:

➢ The localhost is a private address used by computers to refer to themselves. It is


equivalent to the internal IP address [Link] and is only accessible to the local computer
and not to any local networks
[Link] HTTP Module
➢ The port is a number that servers use to connect to a specific process on our IP address as
an endpoint, our web server will run on port 8000.
➢ The server is bound to the host and port, and hence you will be able to access it on
[Link]
[Link] HTTP Module
➢ Create a function requestListener to handle the incoming requests.
➢ This function requires two parameters: a request object and a response object.
➢ The request object stores all of the data from the incoming HTTP request.
➢ The response object is responsible for returning HTTP responses to the server, and it
contains all the details about the response.

➢ The function will return Welcome user!! as a response to the client.


[Link] HTTP Module
➢ [Link](200), specifies the HTTP status code of the response.
➢ HTTP status codes represent how well a server handled an HTTP request.
➢ The status code 200 corresponds to OK in this instance.

➢ [Link]("Welcome user!!"), returns the HTTP response to the client who requested it.
This function returns any data to be sent from the server. It is returning text data in this
case.
[Link] HTTP Module
➢ First, the server object is created using the createServer() function.
➢ The server object accepts all the requests and passes them to the requestListener()
function.
➢ The server is then attached to the port and host using the [Link]() function.
➢ The listen() function takes the port, host and the callback function as arguments. The
callback function is called when the server begins to listen.
[Link] HTTP Module

You might also like