Sikau

Node.js · Day 12 · about 45 min

Project: A structured API

express.Router, modules and validation helpers.

One file doesn't scale

Real APIs split routes into modules with express.Router(): routes/dishes.js, routes/orders.js, mounted under prefixes. Write the dishes router for a café API.

Growing the structure

The router pattern scales smoothly: a new resource means one new file and one new app.use line. As the project grows further, teams often add a layer between routes and data — services such as dishService.list() — so route handlers only deal with HTTP (reading input, choosing status codes) while services hold the business rules. That split makes the rules testable without HTTP at all.

You'll see exactly this layering formalised in the NestJS course next: controllers for HTTP, services for logic, modules to group them. Having built it by hand with Express first, the framework's structure will feel familiar rather than magical.

Keep route files thin: if a handler grows past a screenful, move the logic into a function in lib/ or a service.

The rest of this lesson

  • Structuring an APIexplain
  • Validation helpersexplain
  • Mounted pathpredict
  • Which file?match
  • Building a routerorder
  • Dishes routerchallenge
  • Badge quizquiz
  • Unit 2 complete!recap

Continue this lesson free — run real code in your browser

Continue with Google

See all 18 lessons of Node.js