MEAN Express.js Middleware and Routing 5 — Questions and Answers
Question 1: How can you make an Express route parameter optional?
- Append `?` after the parameter name: `/:id?` (Correct answer)
- Wrap it in brackets: `/[id]`
- Use a wildcard: `/*id`
- Declare it with `app.optional()`
Correct answer: Append `?` after the parameter name: `/:id?`
Appending `?` to a route parameter name, e.g., `/:id?`, makes that segment optional so the route matches both `/users` and `/users/42`.
Question 2: Which statement about `app.use()` path matching is correct?
- The path must match the full URL
- `app.use('/api')` also matches `/api/users` and `/api/products` (Correct answer)
- It only matches GET requests
- Trailing slashes are not allowed
Correct answer: `app.use('/api')` also matches `/api/users` and `/api/products`
`app.use()` performs prefix matching, so mounting on `/api` will match any request whose URL starts with `/api`, including `/api/users`.
Question 3: What does the `router.param()` method do in Express?
- Defines a default value for a route parameter
- Registers a callback that runs whenever a specific route parameter is present (Correct answer)
- Converts route parameters to a query string
- Validates parameters against a JSON schema
Correct answer: Registers a callback that runs whenever a specific route parameter is present
`router.param(name, callback)` adds a pre-processing callback that fires automatically whenever a route containing the named parameter is matched.
Question 4: In Express, which middleware must be added before route definitions to parse `application/x-www-form-urlencoded` POST bodies?
- express.json()
- express.urlencoded({ extended: false }) (Correct answer)
- express.text()
- express.raw()
Correct answer: express.urlencoded({ extended: false })
`express.urlencoded()` parses URL-encoded form data sent in POST requests, populating `req.body` with the parsed key-value pairs.
Question 5: What is a potential security risk of setting `express.static` to serve the project root directory?
- It increases memory usage
- It may expose sensitive files like `package.json` or `.env` to public access (Correct answer)
- It disables gzip compression
- It prevents dynamic routes from working
Correct answer: It may expose sensitive files like `package.json` or `.env` to public access
Serving from the project root means all files — including source code, environment files, and configs — become publicly downloadable.
Question 6: How do you chain multiple route handlers for the same path in Express using `app.route()`?
- app.route('/path').get(h1).post(h2).put(h3) (Correct answer)
- app.chain('/path', [h1, h2, h3])
- app.get('/path', h1); app.post('/path', h2);
- app.route('/path', { get: h1, post: h2 })
Correct answer: app.route('/path').get(h1).post(h2).put(h3)
`app.route()` returns a chainable route object, allowing `.get()`, `.post()`, `.put()` etc. to be chained on a single path definition.
Question 7: When passing an error to `next(err)` in Express, which middleware type intercepts it?
- The next regular middleware in the stack
- Any middleware with two parameters
- The first error-handling middleware (four parameters) defined after the error source (Correct answer)
- The built-in Express 404 handler
Correct answer: The first error-handling middleware (four parameters) defined after the error source
Calling `next(err)` with an argument causes Express to skip all regular middleware and jump to the nearest downstream error-handling middleware that has the `(err, req, res, next)` signature.
How can you make an Express route parameter optional?