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

A route with a param and a JSON parser.

Cheat sheet

Express cheat sheet
Command or codeWhat 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.idPath parameter
req.query.pageQuery string parameter
req.bodyRequest 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

  1. Learn Node.js basics: modules, async and npm.
  2. Take the Express API course on ShortLesson and start a first server.
  3. Understand routes, req, res, HTTP methods and status codes.
  4. Learn to write middleware and an error handler.
  5. Add input validation, for example with zod.
  6. Connect a database and build a full CRUD.
  7. Add authentication with JWT or sessions.
  8. Test the API (supertest) and move to TypeScript.

Interview questions

  1. 1. What is middleware?

    A function that receives req, res and next. It can change the request, respond itself or pass control on by calling next().

  2. 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. 3. Why does the order of app.use matter?

    Middleware runs in the order declared. If you mount the JSON parser after a route, req.body there will be empty.

Comparisons with this technology

Logos belong to their owners and are used for identification only. Icons: Simple Icons (CC0), Devicon (MIT).