Express Middleware Interview Questions and Answers
Common Express middleware interview questions with concise answers.
Express Middleware Interview Questions and Answers
Interviewers ask these middleware questions often. Here are concise answers.
Q1: What is Express middleware?
A function (req, res, next) that runs on requests. It can modify req, send a response, call next to continue, or call next(err) to jump to the error handler. Almost everything in Express is middleware.
Q2: How do you write custom middleware?
Use the (req, res, next) signature. Do work, then call next() or send a response. Use factory functions for configurable middlewares (e.g., validate(schema), requireRole(role)).
Q3: How does Express handle errors?
With a four-parameter middleware (err, req, res, next) registered last. In handlers, throw or call next(err). Express skips to the error handler. Use a custom ApiError class with status and message.
Q4: How do you catch async errors in Express 4?
Wrap with asyncHandler: Promise.resolve(fn(req, res, next)).catch(next). This catches promise rejections and passes them to the error handler. Express 5 catches async errors automatically.
Q5: Why does middleware order matter?
Express runs middlewares in the order you add them. Body parser must come before routes (else req.body is undefined). Helmet should come early (security headers on every response). Error handler must be last (catches all errors).
Q6: What is the difference between app-level and router-level middleware?
app.use(middleware) runs for every request. router.use(middleware) runs only for routes in that router. Use app-level for cross-cutting concerns (json, helmet) and router-level for feature-specific concerns (auth for admin routes).
Q7: How do you protect routes with middleware?
Write an auth middleware that verifies the JWT and sets req.user. Apply with router.use(auth) for a router, or as a per-route middleware. Keep public routes outside.
Q8: How do you validate input with middleware?
Use a factory: validate(schema) returns a middleware that uses zod safeParse on req.body. Returns 400 with errors if invalid, replaces req.body with parsed data if valid, calls next.
Q9: What are common middleware mistakes?
Forgetting next(), calling next() after responding, wrong order, sync heavy work, async middleware without wrapper, and no error handler at all. All cause silent bugs or hangs.
Q10: How do you log requests in Express?
Use morgan for simple logging, pino-http for production. Log method, path, status, and response time. Ship logs to a central place for searching and alerting. Do not use console.log in production.
The Takeaway
Common Express middleware interview answers: middleware is (req, res, next); custom middleware uses factories; errors use a four-parameter middleware; async errors need a wrapper in Express 4; order matters; app-level vs router-level; auth and validation middlewares; common mistakes; logging with morgan or pino-http.
A function (req, res, next) that runs on requests. It can modify req, send a response, call next to continue, or call next(err) to jump to the error handler. Almost everything in Express is middleware.
With a four-parameter middleware (err, req, res, next) registered last. In handlers, throw or call next(err). Express skips to the error handler. Use a custom ApiError class with status and message.
Wrap with asyncHandler: Promise.resolve(fn(req, res, next)).catch(next). This catches promise rejections and passes them to the error handler. Express 5 catches async errors automatically.
Express runs middlewares in the order you add them. Body parser must come before routes (else req.body is undefined). Helmet should come early. Error handler must be last. Wrong order causes silent bugs.
Forgetting next(), calling next() after responding, wrong order, sync heavy work, async middleware without wrapper, and no error handler at all. All cause silent bugs or hangs.
Ready to master Node.js completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master Node.js
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

