Express Router Interview Questions and Answers
Common Express Router interview questions with concise answers.
Express Router Interview Questions and Answers
Interviewers ask these Express Router questions often. Here are concise answers.
Q1: What is express.Router?
A mini-app you can mount with app.use('/path', router). It lets you group routes by feature in separate files. Routers have their own middleware stack and can be nested.
Q2: How do you mount a router?
app.use('/users', require('./routes/users')). The router's paths are then relative to /users. For example, router.get('/:id') responds to /users/:id.
Q3: How do you apply middleware to all routes in a router?
router.use(middleware) at the top of the router file. Every route in the router now runs that middleware. Useful for auth and role checks across a feature.
Q4: Can routers be nested?
Yes. Mount one router inside another: router.use('/:userId/posts', postsRouter). Now /users/:userId/posts/... hits postsRouter. Limit nesting to one level.
Q5: How do you version an Express API with routers?
Mount routers under /api/v1, /api/v2, etc. Each version has its own router. Bump on breaking changes. Non-breaking changes (adding fields) do not need a new version.
Q6: What is the difference between app and router?
app is the Express application. router is a mini-app you mount with app.use. Routers group routes by feature and keep app.js clean.
Q7: How do you handle public vs protected routes in a router?
Put public routes before router.use(auth). Then call router.use(auth) and put protected routes below. This keeps the structure clear and prevents accidental auth on public endpoints.
Q8: How do you handle async errors in router handlers?
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.
Q9: What is the module pattern in Express?
Group each feature into a module: router, controller, service, model, validations in one folder (src/modules/users/). Mount modules in app.js. Gives cohesion, independence, testability, and team scalability.
Q10: What should app.js contain?
Create the app, apply global middlewares (json, cors, helmet), mount routers with app.use, and register the error handler. Everything else lives in feature folders. A thin app.js is a sign of a well-organized app.
The Takeaway
Express Router interview answers: Router is a mini-app mounted with app.use; apply middleware with router.use; nest routers for nested resources (limit to one level); version APIs with /api/v1; app vs router; public routes before router.use(auth); async errors with asyncHandler; module pattern for cohesion; thin app.js.
A mini-app you can mount with app.use('/path', router). It lets you group routes by feature in separate files. Routers have their own middleware stack and can be nested.
router.use(middleware) at the top of the router file. Every route in the router now runs that middleware. Useful for auth and role checks across a feature without repeating per route.
Yes. Mount one router inside another: router.use('/:userId/posts', postsRouter). Now /users/:userId/posts/... hits postsRouter. Limit nesting to one level; for deeper resources, use a separate top-level router with a filter.
Put public routes (signup, login) before router.use(auth). Then call router.use(auth) and put protected routes below. This keeps the structure clear and prevents accidental auth on public endpoints.
Create the app, apply global middlewares (json, cors, helmet), mount routers with app.use, and register the error handler. Everything else lives in feature folders. A thin app.js is a sign of a well-organized app.
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.

