Introduction to Express.js
Last Updated : Oct 2026
Express.js is a web application framework for Node.js. It provides a minimal set of features for building web applications and APIs.It unopinionated about how your app is structured,but most developers Follows a MVC (Model-View-Controller) architecture and some other patterns.
- Model - Data layer,where you store your data or structure your database.
- View - Presentation layer,where you render your data to the user.
- Controller - Logic layer,where you handle user input and logic.
- Router - Routing layer,where you map URLs to controllers.
- Middleware - Middleware layer,where you add additional functionality to your app.
- Utils - Utility layer,Contains helper functions.
- Lib - Library layer,Contains third-party libraries or actions.
- Error - Error handling layer,where you handle errors in your app.
- Config - Configuration layer,where you configure your app.
Note : Each layer is responsible for a specific task and can be separated from the rest of the app.We make folders for each layer,generally named conventions is lowercase in plural form like models,views,etc.
Prerequisites Knowledge for Express.js
- Node.js
- JavaScript
Installation of Express.js
Step 1: Install node.js from nodejs.org.
Step 2: Create a directory for your application and navigate into it.
mkdir myapp
cd myapp
Step 3: Install Express.js using npm.
npm init -y
npm install express
Step 4: Create a basic Express.js app.
create a index.js file and add the following code:
import express from "express";
const app = express();
app.get("/", (req, res) => {
res.send("Hello, Express.js!");
});
app.listen(3000, () => {
console.log("Server started on port 3000 Click http://localhost:3000");
});
Let's break down the code:
- const express = require("express"); imports the express module from the express package.
- const app = express(); creates an instance of the express application.The instance have some common methods and properties.
- app.get() is used to define a route for the HTTP GET method.also same as all other HTTP methods
- app.use() is used to add middleware to the application.
- app.listen() is used to start the server and listen for incoming requests.
- app.set() is used to set configuration options for the application.
- app.all() is used to define a route for all HTTP methods.
- app.route() is used to create chainable routes.
- app.get("/", (req, res) => {...}) defines a route for the root URL (/) with a handler function that sends a response with the text "Hello, Express.js!".
- req is the request object,which contains information about the incoming request.it includes things like headers,query parameters,etc.
- res is the response object,which is used to send the response back to the client.it includes following methods like:
- res.send() sends a response with the specified content.
- res.render() renders a view template with the specified data.
- res.json() sends a JSON response with the specified data.
- res.status() sets the status code for the response.
- res.redirect(status, url) redirects the client to a different URL,with status code.
- res.cookie() sets a cookie in the response.
- res.end() ends the response.
- app.listen(3000, () => {...}) starts the server on port 3000 and logs a message to the console.
Step 5: Change package.json file.
{
"name": "myapp",
"version": "1.0.0",
"type": "module",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"express": "^5.2.1"
}
}
Note: In whole tutorial We always use ESM for importing modules in.You are free to use CJS if you want.
Step 6: Run the app.
node index.js
Step 6: Open your browser and navigate to http://localhost:3000 to see the app running.
Note : You can also use npm start to start the app.
