MEAN Express.js Middleware and Routing 4 — Questions and Answers
Question 1: What does calling `next('route')` do inside an Express route handler?
- Skips remaining handlers in the current route and passes control to the next matching route (Correct answer)
- Calls the global error handler immediately
- Terminates the request-response cycle
- Moves to the next middleware in the same route stack
Correct answer: Skips remaining handlers in the current route and passes control to the next matching route
`next('route')` skips any remaining handlers registered on the current route and jumps to the next route that matches the request.
Question 2: Which Express method binds middleware that runs for every HTTP verb on a specific path?
- app.use()
- app.all() (Correct answer)
- app.route()
- app.any()
Correct answer: app.all()
`app.all()` matches all HTTP methods for a given path, while `app.use()` also matches sub-paths and is intended for mounting middleware.
Question 3: In Express, what is the correct signature for an error-handling middleware function?
- (req, res, next)
- (err, req, res)
- (err, req, res, next) (Correct answer)
- (error, request, response)
Correct answer: (err, req, res, next)
Express identifies error-handling middleware by its four-parameter signature `(err, req, res, next)`, which must be declared exactly this way.
Question 4: What happens if an Express middleware function does not call `next()` and does not send a response?
- Express automatically sends a 500 error
- The request hangs indefinitely (Correct answer)
- Express skips to the next registered route
- The connection is closed after a default timeout
Correct answer: The request hangs indefinitely
If a middleware neither calls `next()` nor ends the response, the request remains open and the client waits until a network or server timeout occurs.
Question 5: Which of the following correctly defines a parameterized route in Express that captures an integer-like segment named `id`?
- app.get('/users/:id(\\d+)', handler) (Correct answer)
- app.get('/users/[id]', handler)
- app.get('/users/?id', handler)
- app.get('/users/{id}', handler)
Correct answer: app.get('/users/:id(\\d+)', handler)
Express supports optional regex constraints on route parameters using the syntax `:param(regex)`, so `/:id(\d+)` restricts `id` to digits.
Question 6: When `express.Router()` is used, how is the router's middleware stack applied to the main app?
- By calling router.listen()
- By passing the router to app.use() (Correct answer)
- By importing it with require() at the top of the file
- By calling app.router.add()
Correct answer: By passing the router to app.use()
A router instance is a mini-application and is mounted onto the main Express app via `app.use(path, router)`.
Question 7: What is the purpose of the `express.static` built-in middleware?
- To parse static JSON bodies
- To serve static files from a specified directory (Correct answer)
- To cache all responses in memory
- To compile TypeScript files on request
Correct answer: To serve static files from a specified directory
`express.static(root)` serves files from the `root` directory, enabling Express to act as a static file server without additional packages.
What does calling `next('route')` do inside an Express route handler?