Full Stack JS

Chapter 12
0%

Chapter 12: Express.js

Build web servers in minutes, not hours — Express.js is the most popular Node.js framework for building APIs and web apps.

What is Express?
Routes
Middleware
Request & Response
Error Handling
Quiz

What is Express?

If Node.js is like having a plot of land, Express is like having a pre-built house frame on that land. You could build everything from scratch with raw Node.js (like building a house brick by brick), but Express gives you the walls, doors, and plumbing already in place. You just decorate (add your routes and logic).

Express simplifies everything about building a server:

Express vs Raw Node.js

Comparison — Raw Node vs Express

Setting Up Express

server.js — Basic Express Setup

Routes

Routes are like doors in a building. Each door (URL) leads to a different room (function). The front door (/) takes you to the lobby. The door labeled "Users" (/api/users) takes you to the user management room. Express lets you label these doors clearly.

Basic Routes

Express — Basic Routes

Route Organization with Router

As your app grows, you split routes into separate files using express.Router():

routes/userRoutes.js — Organized Routes
Rule of thumb: One router file per resource. userRoutes.js handles all user-related endpoints, taskRoutes.js handles tasks, etc. This keeps your server.js clean and organized.

Middleware

Middleware is like security checkpoints at an airport. Before you reach your gate (the route handler), you pass through multiple checkpoints: ticket check, ID verification, baggage scan. Each checkpoint can let you through, stop you, or add something to your boarding pass. Middleware works the same way — each function runs before your route handler.
Express — Middleware
Middleware Flow:

Request → express.json()loggerauthenticateRoute Handler → Response

Each middleware calls next() to pass to the next one, or sends a response to stop the chain.
Forgetting next() is a common bug! If your middleware doesn't call next() or send a response, the request will hang forever. The client will wait and eventually timeout.

Request & Response

Every route handler receives two objects: req (what the client sent) and res (what you send back).

The Request Object (req)

Express — Request Object

The Response Object (res)

Express — Response Object

Error Handling

Error handling is like having a safety net under a tightrope. Things will go wrong — invalid data, missing records, database failures. Instead of letting your app crash, you catch errors gracefully and send helpful messages to the client.
Express — Error Handling
The error handler MUST have 4 parameters: (err, req, res, next). Even if you don't use next, you must include it. That's how Express distinguishes error handlers from regular middleware.
The asyncHandler pattern saves you from writing try/catch in every single route. It's a wrapper that catches any thrown error and passes it to your centralized error handler. Most production Express apps use this pattern.

📝 Chapter 12 Quiz

1. What is Express.js?

A database
A web framework for Node.js
A frontend library like React
A testing tool

2. In the route /api/users/:id, how do you access the id?

req.body.id
req.query.id
req.params.id
req.id

3. What does next() do in middleware?

Passes control to the next middleware/route
Sends a response to the client
Restarts the server
Closes the database connection

4. What does app.use(express.json()) do?

Sends JSON responses
Converts HTML to JSON
Creates a JSON database
Parses incoming JSON request bodies

5. How many parameters does an Express error handler need?

2 (req, res)
4 (err, req, res, next)
3 (req, res, next)
1 (err)
← Chapter 11: Node.js Fundamentals Chapter 13: Sequelize ORM →