What is Node.js
Node.js is a runtime environment that allows you to run JavaScript outside a web browser. It is commonly used for backend development to build fast and scalable server-side applications.
Why Use Node.js
Uses JavaScript for both frontend & backend
Fast and scalable due to its non-blocking architecture
Has a huge library of open-source packages (NPM)
Used by big companies like Netflix, PayPal, and LinkedIn
Installing Node.js
Step 1: Download & Install
- Go to nodejs.org
- Download the LTS (Long-Term Support) version
- Install it (Make sure NPM is included)
Step 2: Check Installation
Open Command Prompt (Windows) or Terminal (Mac/Linux) and type:
node -v
This should show the installed version of Node.js.
To check NPM (Node Package Manager):
NPM -v
Creating Your First Node.js App
1. Create a Project Folder
Open the terminal and run:
mkdir my-node-app CD my-node-app
2. Initialize a Node.js Project
npm init -y
This creates a package.json file, which manages dependencies and configurations.
3. Creating a Simple Server
Create a file called server.js and open it in a code editor (e.g., VS Code).
Write this code in server.js
const http = require("http"); const server = http.createServer((req, res) => { res.writeHead(200, { "Content-Type": "text/plain" }); res.end("Hello, Node.js Server!"); }); server.listen(3000, () => { console.log("Server is running on port 3000"); });
4. Run the Server
terminal, run:
node server.js
5. Test Your Server
http://localhost:3000
You should see “Hello, Node.js Server!”
Using Express.js (Framework)
Express.js is a popular framework for building web applications.
1. Install Express
npm install express
2. Create an Express Server (server.js)
const express = require("express");const app = express(); app.get("/", (req, res) => { res.send("Hello from Express.js!"); }); app.listen(3000, () => { console.log("Server running on port 3000"); });
3. Start the Express Server
node server.js
http://localhost:3000
“Hello from Express.js!”
- Requests:
The request is coming from the client side, and the user wants a response from the server to act on. These actions may be non-blocking, simple, or they may block complex tasks.
- Node.js Server:
Node.js is used on the server side. It takes the incoming client requests from the users, processes the requests, and responds to that particular client or user.
- Event Queue:
The Node.js event queue stores the requests and passes them one by one into the event loop.
- Event Loop:
The event loop receives the request from the event queue, processes the request, and returns the response to the particular client.
- Thread Pool:
The thread pool consists of the available threads in our Node.js server, which will take some tasks to complete the client request.
- External Resources:
External resources are those that need to fulfill the blocking operations in client requests. These resources can be for computation, data storage, etc.
Read Also:
How to Modify WooCommerce Templates
How to Encrypt and Decrypt Data in PHP
Also Visit :
https://inimisttech.com/