Hey Codders! π
Itβs Vikas Sankhla here from Web Codder, and today we are going to make backend coding much easier with Express.js! π
If youβve used Node.js, you know itβs powerful, but sometimes it feels like putting together LEGO without instructions. π§±
π Enter Express.js!
Itβs like getting a ready-made kit that helps you build faster, cleaner, and easier. πΌ
π What Is Express.js?
Imagine youβre building a shop πͺ.
- Node.js gives you the bricks to build.
- Express.js gives you pre-built walls and doors. πͺποΈ
In tech terms:
Express.js is a web framework that sits on top of Node.js and makes it super simple to handle:
- Routes π¦
- Requests & Responses π¬
- Middleware π οΈ
βοΈ Why Use Express.js?
Hereβs a quick before & after:
Without Express.js | With Express.js |
---|---|
Lots of code just to set up! | Quick setup with fewer lines βοΈ |
Manual routing π | Built-in routing π¦ |
Complex request handling | Easy request & response π¦ |
π TL;DR: Express saves time & effort!
π οΈ Setting Up Express.js
1οΈβ£ Install Express
In your project folder, open terminal and type:
npm init -y
npm install express
2οΈβ£ Create app.js
Hereβs the basic code to get started:
jsCopyEditconst express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello from Web Codder! π');
});
app.listen(3000, () => {
console.log('Server running at http://localhost:3000');
});

π Defining Routes
With Express, you can handle different pages (routes) easily.
Example:
jsCopyEditapp.get('/', (req, res) => {
res.send('Home Page π ');
});
app.get('/about', (req, res) => {
res.send('About Page βΉοΈ');
});
This means:
/
β Shows “Home Page π ”/about
β Shows “About Page βΉοΈ”
π Handling Dynamic Data
Want to handle dynamic URLs like /user/John
?
β URL Parameters
jsCopyEditapp.get('/user/:name', (req, res) => {
const name = req.params.name;
res.send(`Hello, ${name}! π`);
});
Now visit http://localhost:3000/user/Vikas
and see:
π “Hello, Vikas! π”
β Query Strings
URL: /search?item=laptop
Code:
jsCopyEditapp.get('/search', (req, res) => {
const item = req.query.item;
res.send(`You searched for: ${item} π`);
});
π οΈ Middleware in Express
π§ What Is Middleware?
Think of middleware like a helper that sits between the request and response. π€οΈ
It can:
- π₯ Check things (e.g., auth)
- π Modify data
- β Stop requests if needed
Example:
jsCopyEditapp.use((req, res, next) => {
console.log('A request was made! ποΈ');
next();
});
Every time a request comes in, youβll see a message in your terminal. π
π§° Built-in Middleware
β body-parser
Handles data from POST requests.
Example:
jsCopyEditapp.use(express.json());
app.post('/data', (req, res) => {
console.log(req.body);
res.send('Data received β
');
});
β cookie-parser (need to install)
bashCopyEditnpm install cookie-parser
Code:
jsCopyEditconst cookieParser = require('cookie-parser');
app.use(cookieParser());
Now you can read cookies πͺ easily!
β Quick Recap Table
Concept | Example Code |
---|---|
Basic Route | app.get('/', (req, res) => { ... }); |
URL Parameter | /user/:name β req.params.name |
Query String | /search?item=laptop β req.query.item |
Middleware | app.use((req, res, next) => { ... }); |
body-parser | app.use(express.json()); |
cookie-parser | app.use(cookieParser()); |
π¨ Visual: Flow of an Express Request

π― Conclusion
Congrats, Codders! π Today you learned:
- β What Express.js is
- β How to build routes easily
- β Handle dynamic URLs & query strings
- β Use middleware like a pro π
π Keep Learning with Web Codder: