01 / 07

What are route methods how to perform tasks for all routes like setting up middleware

Difficulty: 4/10
router methods, middleware, global middleware

A route method is derived from one of the HTTP methods, and is attached to an instance of the express class.

The following code is an example of routes that are defined for the GET and the POST methods to the root of the app.
here is a special routing method, app.all(), used to load middleware functions at a path for all HTTP request methods. For example, the following handler is executed for requests to the route '/secret' whether using GET, POST, PUT, DELETE, or any other HTTP request method supported in the http module.

You can use app.use() to apply middleware ot all routes irrespective of route path or method

Scenario Questions

0-2 years experience

  1. 1You need to log every incoming request's method and URL in an Express app. How would you set that up so it runs for all routes?
  2. 2If you add a middleware with app.use after defining some routes, which routes will it affect and why?
  3. 3How would you apply a middleware only to routes that start with /api?

2-5 years experience

  1. 1We have a feature where certain routes need authentication, but others are public. Walk me through how you'd structure the middleware to enforce this without duplicating code.
  2. 2During a recent deployment, requests to /admin started returning 404 after we added a new global error‑handling middleware. How would you debug the issue?
  3. 3Explain the trade‑offs between using app.use for a catch‑all middleware versus attaching it to each router instance.

5-8 years experience

  1. 1Our service runs on multiple Node processes behind a load balancer, and we need to add a rate‑limiting middleware that works across all routes and processes. What design considerations would you address?
  2. 2We want to conditionally apply a heavy logging middleware only in staging, but keep it off in production for performance. How would you implement this without restarting the server?
  3. 3Describe how the order of route definitions and middleware affects response times and error propagation in a large Express codebase.

8+ years experience

  1. 1The company is migrating a monolithic Express app to a micro‑frontend architecture where each team owns its own router. How would you redesign the global middleware strategy to support independent deployment while preserving cross‑cutting concerns like auth and tracing?
  2. 2Legacy code mixes app.use and router.use inconsistently, causing subtle bugs in error handling. Propose a long‑term refactoring plan that balances risk, testing, and team ownership.
  3. 3At scale, we need to enforce security headers on every response without adding overhead. Discuss architectural options (e.g., proxy vs. middleware) and their impact on maintainability.

Follow-up Questions

  • What happens if you place a middleware after a route handler?
  • How does Express decide the order in which middleware and routes run?
  • Can you describe the difference between regular and error‑handling middleware?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.