Unit 1 Node js
Unit 1 Node js
What is Node.js?
Node.js is an open source server environment
Node.js is free
Node.js runs on various platforms (Windows, Linux, Unix,
Mac OS X, etc.)
Node.js uses JavaScript on the server
Why Node.js?
Node.js uses asynchronous programming!
A common task for a web server can be to open a file on
the server and return the content to the client.
Here is how PHP or ASP handles a file request:
Sends the task to the computer's file system.
Waits while the file system opens and reads the file.
Returns the content to the client.
Ready to handle the next request.
Here is how Node.js handles a file request:
Sends the task to the computer's file system.
Ready to handle the next request.
When the file system has opened and read the file, the
server returns the content to the client.
Node.js eliminates the waiting, and simply continues with
the next request.
Node.js runs single-threaded, non-blocking, asynchronous
programming, which is very memory efficient.
Once you have downloaded and installed Node.js on your computer, let's try
to display "Hello World" in a web browser.
Components of npm
npm mainly consists of three different components,
these are:
1. Website: The npm official website is used to find
packages for your project, create and set up
profiles to manage and access private and public
packages.
2. Command Line Interface (CLI): The CLI runs
from your computer's terminal to interact with npm
packages and repositories.
3. Registry: The registry is a large and public
database of JavaScript projects and meta-
information. You can use any supported npm
registry that you want or even your own. You can
even use someone else's registry as per their
terms of use.
code dependencies.
o Use npm to update applications easily when the
node -v
npm -v
These commands will show the installed versions of
Node.js and NPM.
app.listen(3000, () => {
console.log('Server running at
http://localhost:3000');
});
express() creates an instance of the Express app.
the client.
app.listen(3000) starts the server on port 3000,
Node.js.
Lodash: A utility library delivering consistency,
HTTP requests.
React: A popular front-end library used to build
user interfaces.
What is Node.js REPL?
REPL stands for:
Read: The REPL reads user input (JavaScript code).
screen.
Loop: The process repeats (loops), waiting for the
next input.
This interactive environment allows developers to test small
snippets of code without needing to create a full script. The
REPL is built directly into Node.js, so no additional setup is
required.
The Node.js REPL environment allows developers to test
code snippets quickly. To build real-world full-stack
applications .
Getting Started with REPL
To start working with REPL environment of NODE; open up the
terminal (in case of UNIX/LINUX) or the Command prompt (in
case of Windows) and write node and press ‘enter’ to start the
REPL.
Node
The REPL has started and is demarcated by the ‘>’
symbol. Various operations can be performed on the
REPL. Below are some of the examples to get familiar
with the REPL environment.
Key Features of Node.js REPL
1. Executing JavaScript Code
The REPL is a full-featured JavaScript environment,
meaning you can run any valid JavaScript code inside it.
Example:
commands.
.break: Breaks out of multi-line input or clears the
current input.
.clear: Resets the REPL context by clearing all
declared variables.
.exit: Exits the REPL session.
Example: Performing Arithmetical operations in REPL
Example: Performing operations using libraries of NODE.
MATH library is being used in below example.
What is Blocking?
It refers to the blocking of further operation until the
current operation finishes. Blocking methods are
executed synchronously. Synchronously means that the
program is executed line by line. The program waits
until the called function or the operation returns.
Example: Following example uses
the readFileSync() function to read files and
demonstrate Blocking in Node.js
const fs = require('fs');
What is Non-Blocking ?
It refers to the program that does not block the
execution of further operations. Non-Blocking methods
are executed asynchronously. Asynchronously means
that the program may not necessarily execute line by
line. The program calls the function and move to the
next operation and does not wait for it to return.
Example: Following example uses
the readFile() function to read files and demonstrate
Non-Blocking in Node
const fs = require('fs');
Node.js Callbacks
Callback is an asynchronous equivalent for a function. It
is called at the completion of each task. In Node.js,
callbacks are generally used. All APIs of Node are
written in a way to supports callbacks. For example:
when a function start reading file, it returns the control
to execution environment immediately so that the next
instruction can be executed.
In Node.js, once file I/O is complete, it will call the
callback function. So there is no blocking or wait for File
I/O. This makes Node.js highly scalable, as it can
process high number of request without waiting for any
function to return result.
Blocking Code Example
Follow these steps:
1. Create a text file named input.txt having the
following content:
1. Parul University is NAAC A++ accredited
private university in Vadodara, Gujarat.
Explore our diverse educational programs
2. different technologies, in a very simple langua
ge.
2. Create a JavaScript file named main.js having the
following code:
1. var fs = require("fs");
2. var data = fs.readFileSync('input.txt');
3. console.log(data.toString());
4. console.log("Program Ended");
3. Open the Node.js command prompt and execute
the following code.
1. node main.js
Non Blocking Code Example
Follow these steps:
Create a text file named input.txt having the following
content:
1. Parul University is NAAC A++ accredited private
university in Vadodara, Gujarat. Explore our diverse
educational programs
2. different technologies, in a very simple language.