Backend
Express
A minimalist web framework for Node.js with routing and a middleware chain.
What it is
Express is a thin layer over the http module: routing, convenient request and response handling and a middleware system. Everything else (validation, ORM, auth) you plug in yourself.
Middleware are (req, res, next) functions that run in order. That is how you add logging, JSON parsing, auth and error handling.
It is the most widespread Node framework, so packages exist for almost any task. The downside: you have to invent your project structure.
Use it when
- Small and medium REST APIs where you want full freedom of library choice.
- Learning backend on Node.js.
- Prototypes and BFFs.
Avoid it when
- A large team that needs ready architecture and DI: NestJS fits better.
- Critical throughput at millions of requests: Fastify is usually faster.
Key concepts
- Middleware
- A function in the request chain that calls
next(). - Router
- A group of routes mounted under a prefix.
- Error middleware
- A handler with four arguments
(err, req, res, next). - req / res
- Wrappers over the Node request and response.
Example
Cheat sheet
| Command or code | What it does |
|---|---|
const app = express() | Creates the app |
app.use(express.json()) | Parses the JSON request body |
app.get('/users/:id', handler) | Route with a parameter |
req.params.id | Path parameter |
req.query.page | Query string parameter |
req.body | Request body |
res.status(201).json(data) | Response with a status |
app.use((req, res, next) => next()) | Middleware |
app.use((err, req, res, next) => { }) | Error handler, 4 arguments |
app.listen(3000) | Starts the server on a port |
express.Router() | Separate route module |
How to learn it
- Learn Node.js basics: modules, async and npm.
- Take the Express API course on ShortLesson and start a first server.
- Understand routes, req, res, HTTP methods and status codes.
- Learn to write middleware and an error handler.
- Add input validation, for example with zod.
- Connect a database and build a full CRUD.
- Add authentication with JWT or sessions.
- Test the API (supertest) and move to TypeScript.
Interview questions
1. What is middleware?
A function that receives
req,resandnext. It can change the request, respond itself or pass control on by callingnext().2. How do you handle errors in Express?
Create a middleware with four arguments and mount it after all routes. In async handlers, pass the error to
next(err).3. Why does the order of
app.usematter?Middleware runs in the order declared. If you mount the JSON parser after a route,
req.bodythere will be empty.
Comparisons with this technology
Related technologies
- Node.jsA server-side JavaScript runtime built on the V8 engine with an event-driven, non-blocking I/O model.
- FastifyA fast Node.js framework with a plugin architecture and JSON Schema validation.
- NestJSA progressive TypeScript Node.js framework with modules, decorators and dependency injection.
- JWTA compact signed token in JSON format that carries claims about a user or permissions.
Logos belong to their owners and are used for identification only. Icons: Simple Icons (CC0), Devicon (MIT).