Express Routing Interview Questions and Answers
Common Express routing interview questions with concise answers.
Express Routing Interview Questions and Answers
Interviewers ask these Express routing questions often. Here are concise answers.
Q1: What is a route in Express?
A binding between an HTTP method, a URL path, and a handler function. app.get('/users/:id', getUser) is a route. Express dispatches the matching handler when a request comes in.
Q2: What is the difference between app and router?
app is the Express application. router is a mini-app you can mount with app.use('/path', router). Routers group routes by feature and keep app.js clean.
Q3: How do route params, query params, and body differ?
Route params identify a resource (/users/:id, in path, required). Query params filter (/users?role=admin, optional). Body sends data for create/update (POST, PATCH, with express.json).
Q4: How does Express match routes?
In declaration order. The first matching route wins. This is why specific routes must come before wildcards, and /me before /:id.
Q5: How do you handle async errors in route handlers?
In Express 4, wrap with asyncHandler that catches promise rejections and calls next(err). In Express 5, async errors are caught automatically. Use a custom ApiError class with status and message.
Q6: What is the role of next()?
Passes control to the next middleware or handler. Without next(), the request hangs. With next(err), Express skips to the error handler.
Q7: How do you mount a router?
app.use('/users', require('./routes/users')). The router's paths are then relative to /users.
Q8: How do you apply middleware to specific routes only?
Pass it as an argument: router.get('/me', auth, getMe). You can stack multiple middlewares before the handler.
Q9: How do you version an Express API?
Mount routers under /api/v1, /api/v2, etc. Each version has its own router. Bump the version on breaking changes. Non-breaking changes (adding fields) do not need a new version.
Q10: How do you protect routes with auth?
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. Use role middlewares for admin.
The Takeaway
Express routing interview answers: route = method + path + handler; app vs router; params vs query vs body; first match wins; async errors with a wrapper; next passes control; mount with app.use; per-route middleware; version with /api/v1; auth middleware protects routes.
A binding between an HTTP method, a URL path, and a handler function. app.get('/users/:id', getUser) is a route. Express dispatches the matching handler when a request comes in.
In declaration order. The first matching route wins. This is why specific routes must come before wildcards, and /me before /:id. Wrong order causes silent bugs where routes never get hit.
In Express 4, wrap with asyncHandler that catches promise rejections and calls next(err). In Express 5, async errors are caught automatically. Use a custom ApiError class with status and message.
It passes control to the next middleware or handler. Without next(), the request hangs. With next(err), Express skips to the error handler. Every middleware must call next() or send a response.
Mount routers under /api/v1, /api/v2, etc. Each version has its own router. Bump the version on breaking changes. Non-breaking changes (adding fields) do not need a new version.
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.

