Building APIs with Express

Build a typed REST API with Express: routing, middleware, a layered structure and centralised error handling.

Intermediate · about 10 hours · difficulty 3/5

Learn first: Node.js Runtime Essentials, REST API Design

Express is the most widely used Node.js web framework. It is minimal, so learning it teaches you what every backend framework does under the hood. Express 5 handles rejected promises from async handlers, which removes a classic source of crashes.

Core concepts

  • Routing: app.get, router.post and route parameters like /companies/:id.
  • Middleware: functions that run in order for each request. Use them for JSON parsing, CORS, logging, auth and rate limiting.
  • Error middleware: one handler with four arguments at the end of the chain turns thrown errors into consistent JSON responses.

Structure that scales

Split code into routes (HTTP only), controllers or handlers (parse input, call services), services (business logic) and data access (Prisma or Drizzle). Group by feature (students/, companies/) rather than by type once the app grows.

Must-have middleware

  • express.json with a body size limit.
  • cors configured for your actual frontend origin.
  • helmet for security headers.
  • A rate limiter on login and signup routes.

Pitfalls

  • Business logic and SQL inside route files, making everything hard to test.
  • Sending a response twice (forgetting to return after res.json).
  • CORS set to allow every origin with credentials.

You are ready to move on when you have an Express + TypeScript API for students and companies with feature folders, validation middleware, central error handling and security middleware.

Key concepts

  • Routing and route params
  • Middleware chain
  • Error-handling middleware
  • Layered structure: routes, services, data
  • cors and helmet
  • Rate limiting
  • Express 5 async errors

Other options for this step

Mini project

Build an Express + TypeScript API for the placement portal with CRUD for companies and openings, Zod validation middleware, a central error handler, helmet, CORS and rate limiting on auth routes.

Check yourself

Where in the middleware order should authentication run relative to body parsing and your route handlers, and why?

Free resources