Express.js is a lightweight Node.js framework for building web applications and REST APIs.
- Simplifies routing and HTTP request handling.
- Supports middleware for extending application functionality.
- Enables fast and scalable server-side development.
System Requirements:
Getting Started with Express.js
Follow these steps to create and run your first Express.js application.
Step 1: Create a Project Directory
Create a new folder for your Express application and navigate into it.
Open Command Prompt or Terminal and run:
mkdir my-express-app
cd my-express-app
Step 2: Initialize the Project
Initialize a new Node.js project.
npm init -yThis command creates a package.json file that stores your project's metadata and dependencies.

Step 3: Install Express
Install Express as a project dependency.
npm install express
Your package.json will include Express under dependencies.
Step 4: Create the Server File
Create a file named app.js (or server.js) in the project directory.
Project Structure

Add the following code:
const express = require("express");
const app = express();
const PORT = 3000;
app.get("/", (req, res) => {
res.send("Hello, Express!");
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
- Import the Express module using require().
- Create an Express application with express().
- Define the server port.
- Create a GET route for the root (/) URL.
- Start the server using app.listen().
Step 5: Run the Application
Start the server.
node app.js
Open your browser and visit:
http://localhost:3000
Step 6: Add Routes
Routes define how your application responds to different HTTP requests.
Syntax
app.METHOD(path, callback);Example: GET Route
app.get("/", (req, res) => {
res.status(200).send("Welcome to the Home Page");
});
Example: Another GET Route
app.get("/hello", (req, res) => {
res.set("Content-Type", "text/html");
res.status(200).send("<h1>Hello Express!</h1>");
});
Visit:
http://localhost:3000/hello
The browser renders the HTML response.
Example: POST Route
app.post("/submit", (req, res) => {
res.send("Form Submitted");
});
Common HTTP methods include:
- GET
- POST
- PUT
- PATCH
- DELETE
Step 7: Receive Data from the Client
Use the built-in JSON middleware to parse incoming JSON requests.
const express = require("express");
const app = express();
app.use(express.json());
app.post("/", (req, res) => {
const { name } = req.body;
res.send(`Welcome ${name}`);
});
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server is running at http://localhost:${PORT}`);
});
You can test this endpoint using tools like Postman, Thunder Client, fetch(), Axios, or cURL.
Example request body:
{"name": "Ryan"}
Response:

Step 8: Send Different Types of Responses
Express provides several response methods.
Send Plain Text
res.send("Hello World");Send JSON
res.json({
success: true,
message: "Data received"
});
Set Status Code
res.status(201).send("User Created");Set Response Headers
res.set("Content-Type", "text/html");Step 9: Serve Static Files
To serve CSS, JavaScript, images, or HTML files, use the express.static() middleware.
Project Structure:

In app.js:
const express = require("express");
const path = require("path");
const app = express();
app.use(
"/static",
express.static(path.join(__dirname, "Static Files"))
);
app.listen(3000, () => {
console.log("Server is running at http://localhost:3000");
});
Access files using:
http://localhost:3000/static/index.html
Step 10: Send a Single File
Use sendFile() when you want to send one specific file.
const express = require("express");
const path = require("path");
const app = express();
const PORT = 3000;
app.get("/file", (req, res) => {
res.sendFile(path.join(__dirname, "Static Files", "image.jpg"));
});
app.listen(PORT, () => {
console.log(`Server is running at http://localhost:${PORT}`);
});
Visit:
http://localhost:3000/file