03 / 04

List some common middleware tasks?

Middleware in Express (or other web frameworks) is like the bouncer, gatekeeper, or stage crew — it handles stuff before or between your app's logic and the request/response cycle. Here are some common things middleware is used for:

  1. 1

    Logging

  2. 2

    Authentication and Authorization

  3. 3

    Handling CORS (Cross-Origin Resource Sharing)

  4. 4

    Parsing Request Body

  5. 5

    Serving Static Files

  6. 6

    Request Rate Limiting

  7. 7

    Error Handling

  8. 8

    Custom Headers / Response Modification

  9. 9

    Compression

Difficulty: 5/10
Topics: logging, authentication, error handling

Scenario Questions

0-2 years experience
  1. 1

    How would you add a middleware to log each incoming request's method and URL in an Express app?

  2. 2

    If you place a body‑parsing middleware after a route handler, what will happen when a client sends JSON data?

  3. 3

    What does calling next() do inside a middleware function?

2-5 years experience
  1. 1

    You need to protect certain routes with JWT authentication. Walk me through how you'd set up the middleware and what trade‑offs you consider.

  2. 2

    During a recent deployment, requests started returning 500 errors after you added a new error‑handling middleware. How would you debug the issue?

  3. 3

    Explain why the order of static file serving middleware and custom logging middleware matters in a typical Express app.

5-8 years experience
  1. 1

    Our API gateway uses Express and runs on many instances. How would you design middleware for rate limiting that scales without becoming a bottleneck?

  2. 2

    We have both synchronous and asynchronous error handling middleware. How do you ensure errors propagate correctly across async code paths?

  3. 3

    Discuss the performance impact of using heavy body‑parsing middleware on high‑traffic endpoints and how you might mitigate it.

8+ years experience
  1. 1

    We are migrating a monolithic Express service to a micro‑services architecture. How would you refactor shared middleware (e.g., auth, logging) to avoid duplication across teams?

  2. 2

    Across multiple services, we need consistent request tracing. What architectural approach would you take to implement distributed tracing middleware while keeping services loosely coupled?

  3. 3

    If a legacy Express app uses many custom middleware functions that are hard to test, how would you restructure them to improve maintainability and enable automated testing at scale?

Follow-up Questions

  • Can you walk me through a minimal code example?
  • What pitfalls have you run into when chaining multiple middlewares?
  • How would you verify that your middleware behaves correctly in production?