Introduction to Node.
js, Understanding Chrome V8
Engine, Client-Server Architecture.
Introduction to Node.js
Node.js is an open-source, cross-platform runtime environment that allows you to run JavaScript code
outside of the browser. It was created to allow developers to use JavaScript for server-side programming,
meaning you can write both client-side (in the browser) and server-side code in the same language.
In a typical web application, JavaScript is used to handle client-side (frontend) tasks, like handling user
input or displaying content dynamically. However, with Node.js, you can use JavaScript to handle server-
side tasks like handling database queries, managing files, or creating APIs.
Key Features of Node.js:
1. Non-blocking I/O: Node.js uses non-blocking, event-driven architecture, which means it can
handle multiple requests at the same time without waiting for one request to complete before
moving to the next.
2. Single-threaded: Unlike traditional server-side technologies, Node.js operates on a single thread
and manages concurrency using an event loop.
3. Scalable: Because of its non-blocking I/O and event-driven architecture, Node.js is highly
scalable and efficient.
Real-world example:
Suppose you're building a website, and someone sends a request to fetch user data from a database. In
other server-side environments (like PHP), the server will block the process until the data is retrieved.
With Node.js, it will fetch the data in the background and can serve multiple users simultaneously,
without waiting for each task to finish.
Understanding Chrome V8 Engine
The Chrome V8 engine is a high-performance JavaScript engine developed by Google. It powers the
JavaScript execution in both Google Chrome and Node.js.
How V8 Works:
V8 takes your JavaScript code and compiles it into machine code (the code your computer can
directly execute) rather than interpreting it line by line. This process makes JavaScript run faster.
It uses techniques like Just-In-Time (JIT) compilation, which compiles frequently-used functions
into optimized machine code while the code is being executed.
Key Features of V8:
1. Speed: Since it compiles directly into machine code, JavaScript executes faster.
2. Memory Management: V8 has built-in garbage collection to automatically free up memory
when it's no longer needed, which helps keep the server running efficiently.
Real-world example:
Imagine you're running a JavaScript-heavy website like Gmail. The V8 engine will optimize and execute
the JavaScript quickly so the website feels fast and responsive even with a lot of features running in the
background.
Client-Server Architecture
Client-Server Architecture is the foundation of the web. It describes how the internet works by
separating systems into two main components: clients and servers.
1. Client:
o The client is the user-facing side, such as a browser or mobile app. The client sends
requests (like clicking a link or submitting a form) to the server.
o Examples of clients: Web browsers (Chrome, Firefox), mobile apps, or desktop software.
2. Server:
o The server is where the actual data and services reside. It processes the client's request,
performs the necessary operations (like querying a database), and then sends a
response back to the client.
o Servers are powerful machines that handle many requests at once. They serve web
pages, process data, and store files or databases.
How it works:
Request-Response Cycle:
o The client sends a request (like "get me this webpage") to the server.
o The server processes the request and sends a response (like "here's the webpage").
API Communication: Clients and servers often communicate using APIs (Application
Programming Interfaces). APIs allow the server to send data back in formats like JSON.
Real-world example:
When you type in www.example.com in your browser (client), it sends a request to a server hosting the
example.com website. The server processes the request and sends back the necessary HTML, CSS, and
JavaScript files, which your browser then uses to display the website.
Node.js in Client-Server Architecture
With Node.js, you can easily create the server that handles requests from clients. It acts as the server-
side part in the client-server model. Because of Node.js's non-blocking nature, it can handle thousands
of clients simultaneously, making it ideal for real-time applications (like chats or games).
Summary
Node.js is a platform that lets you run JavaScript on the server (not just in the browser). This
allows you to use JavaScript for both the frontend (client-side) and backend (server-side) of a
web application.
Chrome V8 Engine is a fast JavaScript engine developed by Google. It takes your JavaScript code
and turns it into machine code so it runs quickly. This engine is used in both the Chrome browser
and Node.js.
Client-Server Architecture is how the internet works. The client (like a browser) sends requests
to the server (where data and services are stored), and the server processes these requests and
sends responses back to the client.
Installation of Node.js, VS Code, Postman
Project Setup, Initializing npm, Introduction to npm and package.json
1. Download and install vs code
https://code.visualstudio.com/docs/?dv=win64user
2. Download and install postman
https://www.postman.com/downloads/
After installation, login Postman using your Gmail account.
Postman is a software tool used to test APIs. It lets you send requests like GET, POST, PUT, and DELETE
to your server and see the responses.
For example:
You can check if your backend server (Node.js and Express) is working properly.
If you're trying to fetch data from MongoDB, you can send a request in Postman and see if the
data is coming correctly.
Node.js Installation Guide:
https://www.twilio.com/docs/usage/tutorials/how-to-set-up-your-node-js-and-express-development-
environment
3. Download and Install node.js
https://nodejs.org/en/#download
4. How to check the installed software:
Open command prompt, run it as administrator, and write the name of software, before
software name type –
For example: node -v, npm -v
Create new project:
5. Create a folder/directory in the location on your computer where you want to create your
project.
6. You need to open this folder in VS Code.
7. Now go to vs code toolbar. Click on three dots > Open Terminal > Open New Terminal.
Note: The Terminal is a built-in command-line interface where you can execute commands without
leaving the editor. The terminal lets you use commands to install or manage anything inside this
folder. It's for doing tasks using commands.
8. Now, run the command in this terminal: npm init
You will see the message like:
This utility will walk you through creating a package.json file.
It only covers the most common…….
9. Now set your package name, version, etc. detail and keep pressing Enter until the package.json
file is created in your directory.
Note: Do not rename the package.json file.
10. You can create here your index file for example index.js.
11. Install express js (Guid: https://www.npmjs.com/package/express)
Go to terminal and type the command: npm i express
(added 65 packages, and audited 66 packages in 6s)
This command perform the following:
Installs Express: Downloads the Express framework from the NPM registry and adds it to your
project.
Updates package.json: Automatically adds Express as a dependency in the package.json file, so
it’s listed as part of your project.
Creates node_modules: Puts the Express library (and any of its dependencies) into the
node_modules folder in your project.
Updates package-lock.json: Saves the exact version of Express and its dependencies, so
everyone working on the project uses the same versions.
In simple terms: Running npm i express installs Express into your project, so you can use it to create a
web server or API.
NPM (Node Package Manager)
NPM is a package manager for JavaScript, especially used with Node.js. It helps developers to manage
libraries (called "packages") that are used in their projects. In simpler terms, it’s like a marketplace where
you can download ready-made code (modules) to use in your application, saving time and effort.
Here’s a quick breakdown of what NPM is:
What NPM Does:
1. Package Management: You can install, update, and manage packages (libraries) in your project.
2. Dependency Management: When you use a package that requires other packages
(dependencies), NPM automatically installs them for you.
3. Publishing Packages: You can also publish your own code as a package for other developers to
use.
How NPM Works:
1. Packages: These are reusable pieces of code (modules) that you can include in your project. For
example, if you need a library to handle HTTP requests, you can use a package like express.
2. package.json: This is a file in your project that lists all the dependencies and other details about
your project. NPM uses this file to know which packages to install.
3. Installing Packages: You can install a package by running a simple command in the terminal:
Example: npm install <package-name>
For example, to install express, you would type: npm install express
Note: Once Express is installed, its details will appear in the dependencies section of the package.json
file.
Install and configure nodemon for automatic server restarts.
Nodemon is a tool that helps develop Node.js-based applications by automatically restarting the server
whenever file changes in the directory are detected. This means you don't have to manually stop and
restart the server each time you modify the code. It saves time and speeds up the development process.
When you make any changes in the code, the terminal/browser will still show the previous output until
you stop and restart the server.
To turn off the server: Ctrl + C
To turn on the server: node index.js
1. install node mon using link:
https://www.npmjs.com/package/nodemon
2. command: npm i nodemon or npm i nodemon -g
3. Version: nodemon -v
There might be an issue with one of the windows here.
Step 1 : Go to this location --> File C:\Users\Dell\AppData\Roaming\npm
Step 2 : Delete the nodemon.ps1 file and run the command.
4. Now we will run the server using nodemon: nodemon index.js
Create a simple Node.js server that listens on a specified port and responds with
a "Hello World" message for any incoming requests.
// First, we need to start the server
// Go to the terminal and type node index.js
//To stop server: ctrl + c
// Use nodemon to start server: nodemon index.js
//Now, you need to run this project on a specific port.
Index.js:
const express = require('express')
//require express
const app = express()
// We created a variable and assigned the Express function (or whatever Express had) to the variable
"app."
const port = 3000
//port created
app.listen(port, ()=>{
console.log("server is open now")
})
// We used the listen function to run the server. In the first parameter, we pass the port, and in the
second parameter, we pass a callback function. This is used so that the compiler waits here to see what
is inside it, like any output. For example, we have provided output inside the callback function here.
//In Node.js, asynchronous behavior means it doesn’t wait for one line to finish before moving to the
next. A callback function is used to handle this, displaying output when the port is successfully listened
to.
app.get("/page1", (req, resp)=>{
resp.send("helo word")
})
// We created an API using the GET method.
// The first parameter is the route name, and the second is the callback function.
// The callback function has two parameters:
// 1st (req) is for the request from the front end,
// 2nd (res) is for the response sent back to the front end.
// This structure will be the same for every API we create.
//Now go to browser and type this url:
http://localhost:3000/page1
// There are 5 API methods: GET, POST, DELETE, PUT, and PATCH.
// GET is used to retrieve data from the server to the browser.
A callback function is a function that is passed as an argument to another function and is executed
after the completion of that function. In JavaScript, it's often used for handling asynchronous tasks like
reading files, making API calls, or handling events.
Use Postman to send a GET request to the server and verify that it responds
with the "Hello World" message.
Postman is a popular tool used to test APIs, including those built with Node.js. It allows developers to
send HTTP requests (like GET, POST, PUT, DELETE, etc.) to a server and view the responses.
>Open postman
> Select get
> give url
>Press send
>The API response can be checked.