Express.js is a minimalist yet powerful framework for building APIs and backend applications. In this second part of the series, you’ll learn how Express works, how routing functions, how middleware behaves, and how to build your first structured API endpoint.
What Is Express.js (Fun Version)
Imagine Node.js as a raw block of marble. Powerful, solid, but rough. Express.js is the sculptor that turns that marble into a masterpiece.
It gives you:
- Cleaner routing
- Middleware superpowers
- Readable code structure
- Faster development
- A massive ecosystem
Express is small but mighty — like a pocket knife that somehow contains a chainsaw.
Installing Express.js
Inside your project folder:
npm init -y
npm install express
This adds Express to your project and creates a package.json file to track dependencies.
Building Your First Express Server
Create:
app.js
Add this:
const express = require('express');
const app = express();
// Middleware to parse JSON
app.use(express.json());
// Home route
app.get('/', (req, res) => {
res.send('Welcome to your first Express API!');
});
// Example API route
app.get('/api/hello', (req, res) => {
res.json({ message: 'Hello from Express!' });
});
// Start server
app.listen(3000, () => {
console.log('Express server running on http://localhost:3000');
});
Run it:
node app.js
Visit:
- http://localhost:3000
- http://localhost:3000/api/hello
You now have a real API.
Breaking Down the Magic
1. Creating the App
const app = express();
This is like summoning your API into existence.
2. Middleware
app.use(express.json());
Middleware is like a bouncer at a club — it checks requests before they enter your routes.
Examples of middleware tasks:
- Parsing JSON
- Logging requests
- Authenticating users
- Handling errors
3. Routes
app.get('/api/hello', ...)
Routes are like doors. Each door leads to a different response.
4. Starting the Server
app.listen(3000)
Your API opens for business.
Adding More Routes (Because You’re a Backend Chef Now)
app.post('/api/user', (req, res) => {
const { name, email } = req.body;
res.json({
message: 'User created successfully!',
user: { name, email }
});
});
Try sending a POST request using Postman or Thunder Client.
Understanding Middleware (The Secret Sauce)
Middleware functions look like this:
app.use((req, res, next) => {
console.log(`${req.method} ${req.url}`);
next(); // Move to the next middleware or route
});
Think of middleware as:
- Filters
- Guards
- Helpers
They run before your routes and can modify requests, responses, or block access entirely.
Why Express.js Is Loved by Developers
- Minimalist but powerful
- Flexible structure
- Huge community
- Perfect for REST APIs
- Easy to learn
Sources & Further Reading
- Express.js Official Docs — https://expressjs.com
- Node.js Docs — https://nodejs.org/en/docs
- MDN HTTP Guide —
https://developer.mozilla.org/en-US/docs/Web/HTTP(developer.mozilla.org in Bing)

