04 - Learn How Express.js Works with Node.js in Easy Steps


Introduction
Node.js is the JavaScript runtime environment, not a framework, which allows running JavaScript on any computer.
Express.js is a popular web framework for JavaScript used to write the JavaScript backend.
It increases the readability of the code.
It allows writing less code instead of a large codebase.
Ability to add a middle layer.
Express.js mainly provides ease and efficiency, but you can build a backend without it using pure Node.js.
Handles the requests and responses.
Node.js is like a manual screwdriver, whereas Express.js acts as an electric screwdriver.
Writing code using Express.js with Node.js gives more power to write backend more efficiently.
npm install express
: to install the express package.
Why Use Express.js?
Fast & Minimal – Express.js is lightweight and doesn’t add unnecessary complexity.
Easy Routing – Helps define API routes for handling different HTTP requests.
Middleware Support – Allows adding extra functionalities like authentication, logging, and error handling.
Built-in HTTP Methods – Simplifies handling GET, POST, PUT, DELETE requests.
Integration – Easily connects with databases (MongoDB, MySQL) and templating engines.
Scalability – Helps build large applications with a structured codebase.
Community & Packages – Large community support with many npm packages available.
Creating First server with Exprees.js
Server-Side Backend is made by server, application and databases ,called as server-side
Client-Side : User interact and access server from the client-side.
Client access the server using internet.
Creating an Express Server
Create directory
Create
index.js
Initialize npm.
Install the Express package
Write Server application in index.js
Start server
- index.js
import express from "express";
const app = express();
const port = 3000;
app.listen(port, ()=>{
console.log("Server running on port 3000.");
})
Localhost
The server is not present on the internet but is hosted locally on our computer.
Example:
https://localhost:3000
Port
A port is the location of the server.
It is the door to access the server.
Each port is identified uniquely.
There are many ports to access the server to listen to different incoming requests.
We can find which port is listening on the server using:
Windows:
netstat -ano | findstr "LISTENING"
Mac/Linux:
sudo lsof -i -P -n | grep LISTEN
HTTP Request
Hyper Text Transfer Protocol
It allows communication/connection between different servers/computers across the internet.
GET
: Retrieves information from the specified resources, and should be only used to request the data(not modify it).POST
: Send data to the server for the processing.PUT
: Completely replace the resources.PATCH
: Updates parts of existing resources.DELETE
: Delete the specified resources from server.
import express from "express";
let app = express();
const port = 3000;
// Request, response
// Getting resources from the browser
app.get("/", (req, res) => {
// Sending resources to the server
res.send("Hello World!");
});
//sending about
app.get("/about", (req, res) => {
res.send("<h1>About Us</h1><p>I'm Darshan</p>");
});
// sending Contact
app.get("/contact", (req, res) => {
res.send("<h1>Contact</h1>");
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
nodemon
It is a tool that helps develop Node.js-based applications by automatically restarting the node application when file changes in the directory are detected.
This means when changes are made to the file, the server automatically stops and restarts again.
npm install -g nodemon
:g
stands for global (It will be installed for all directories).nodemon index.js
: To run/start the server.
HTTP Response Status Code
- HTTP response status codes indicate whether a specific HTTP request has been successfully completed. Responses are grouped in five classes:
Informational responses (100 – 199)
Successful responses (200 – 299)
Redirection messages (300 – 399)
Client error responses (400 – 499)
Server error responses (500 – 599)
Postman
- Postman is used when we have to create backend without front end
Subscribe to my newsletter
Read articles from Darshan Bagade directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
