NODEJS Contents

Routing Patterns (params, query, nesting)

Production routing is about clear boundaries and predictable behavior: define routes as contracts and keep business logic out of handlers.

On this page

Routing as a Contract

Routes define your public API. Treat them as contracts: method + path + input + output. In production, handlers should be thin adapters that validate input, call services, and format output.

Router Composition

Group routes by domain, mount them under versioned prefixes, and avoid a single giant file. Route modules should be easy to test and reason about.

import { Router } from 'express';
import type { Request, Response } from 'express';

export const usersRouter = Router();

usersRouter.get('/', async (_req: Request, res: Response) => {
  res.json({ items: [] });
});

Routing Pitfalls

  • Ambiguous route patterns that shadow each other
  • Multiple response paths without a single return pattern
  • Mixing validation, DB access, and formatting in the handler

Performance Note

Routing overhead is rarely the bottleneck. Your bottlenecks are typically I/O, serialization, and upstream dependencies. Optimize routing only after measuring.