06 / 07

What is the purpose of express.Router class?

Use the express.Router class to create modular, mountable route handlers. A Router instance is a complete middleware and routing system; for this reason, it is often referred to as a “mini-app”.

The following example creates a router as a module, loads a middleware function in it, defines some routes, and mounts the router module on a path in the main app.
import and use in app
Difficulty: 4/10
Topics: modular routing, middleware grouping, route organization

Scenario Questions

0-2 years experience
  1. 1

    You need to add a new set of user profile endpoints to an existing Express app. How would you use express.Router to keep the code organized?

  2. 2

    If you forget to export the router from a module and import it in app.js, what will happen when the server starts?

  3. 3

    How does mounting a router at '/api' affect the paths defined inside the router?

2-5 years experience
  1. 1

    We have a feature where some routes need authentication middleware and others don't. How would you structure routers to apply auth only where needed without duplicating code?

  2. 2

    During a code review you notice that a route handler is defined directly on app instead of a router, and the team is getting tangled dependencies. How would you refactor using express.Router, and what pitfalls might you watch for?

  3. 3

    Our API versioning uses '/v1' and '/v2' prefixes. Explain how you would set up routers to support both versions and what issues could arise if you mount them incorrectly.

5-8 years experience
  1. 1

    In a large codebase we’re seeing increased memory usage and slower cold starts because each route file imports the entire app. How would you redesign the routing layer with express.Router to improve startup time and maintainability?

  2. 2

    We need to add per‑router error handling and logging while keeping global error handling consistent. How can you leverage express.Router to isolate concerns, and what trade‑offs does this introduce?

  3. 3

    When scaling to many micro‑services, we want to share common route definitions across services. How would you use express.Router to enable reuse, and what challenges around versioning and dependency management might you face?

8+ years experience
  1. 1

    Our organization is moving from a monolithic Express server to a distributed set of services, but we still need a unified API gateway. How would you design the router hierarchy and module boundaries now to ease that migration, and what long‑term architectural considerations should you keep in mind?

  2. 2

    Several teams maintain their own routers that are mounted on the same base path, leading to route collisions. How would you establish a routing contract or naming convention using express.Router to prevent conflicts at scale?

  3. 3

    We plan to introduce a plugin system where third‑party modules can register routes dynamically at runtime. How would you structure the use of express.Router to safely isolate plugins and allow hot‑loading, and what security or performance implications must you address?

Follow-up Questions

  • What happens if you call next() inside a router but there is no matching downstream route?
  • How does middleware order differ between app‑level and router‑level registrations?
  • How would you unit‑test that a router is correctly mounted at its intended base path?