Sample questions
Question 1Easy
Which property holds the value of q for a request to /search?q=node?
- req.params.q
- req.query.q
- req.body.q
- req.headers.q
Answer: B. req.query.q
Explanation
Query-string values are parsed into req.query; req.params holds named path segments such as :id.
Step-by-step solution
- req.query parses URL parameters like ?q=node.
- req.params reads path segments like /search/:q.
- req.body is for POST/PUT data, not URLs.
- req.headers contains HTTP headers, not query values.
- The value of q comes from req.query.q.
Why the other options are wrong
- A. req.params holds route path parameters such as /users/:id, not query-string values.
- C. req.body holds the parsed request body of POST or PUT, not the URL query string.
- D. req.headers holds HTTP header fields, not query-string values.
Tip: Use req.query for GET parameters in URLs.
Question 2Easy
What is express.static('public') used for?
- Serving files such as CSS, images and JS from a folder
- Caching database query results in memory
- Compiling templates ahead of time
- Declaring global variables for all routes
Answer: A. Serving files such as CSS, images and JS from a folder
Explanation
express.static maps request paths to files in the given directory and serves them with suitable headers.
Step-by-step solution
express.static('public') tells Express to serve files from the 'public' folder.- It handles requests for CSS, images, and JavaScript files automatically.
- The middleware sends these files with correct headers for browsers to use them.
- This makes static assets available without extra code.
- So it is used for serving files such as CSS, images and JS from a folder.
Why the other options are wrong
- B. express.static serves files from disk; it does not cache database query results.
- C. Template compilation is done by a view engine, not by express.static.
- D. Global variables are not declared by express.static; it only maps a folder to URLs.
Tip: Use express.static to serve static files easily in Express apps.
Question 3Easy
What is the signature of a normal (non-error) Express middleware function?
- (err, req, res, next)
- (req, res, next)
- (request, callback)
- (app, req, res)
Answer: B. (req, res, next)
Explanation
Middleware receives the request, the response and next, which passes control to the next middleware in the stack.
Step-by-step solution
- Middleware function takes req, res, next as parameters.
- Req is the request object, res is the response object.
- Next is a function to pass control to the next middleware.
- This signature allows middleware to handle requests and call next.
- It does not include err parameter for normal functions.
- Final answer: (req, res, next)
Why the other options are wrong
- A. err is the first parameter of an error handler, so a normal middleware does not take it.
- C. Express middleware is not called with a request and callback pair; it uses req, res and next.
- D. app is not passed as an argument to middleware; req and res are the request and response.
Tip: (req, res, next) is the standard signature for normal middleware in Express.
Question 4Easy
req.body is undefined for JSON POST requests in an Express app. What is the most likely cause?
- The client used POST instead of PUT
- JSON bodies are only available in
req.query, never in req.body - The route path is missing a trailing slash
- No body-parsing middleware such as
express.json()
Answer: D. No body-parsing middleware such as express.json()
Explanation
Express does not parse request bodies by itself; express.json() reads the stream and fills req.body.
Step-by-step solution
- Express does not automatically parse JSON bodies in POST requests.
- You must use middleware like
express.json() to read the body. - Without this,
req.body remains undefined. - This matches option D exactly.
- Final answer: D.
Why the other options are wrong
- A. PUT and POST are both allowed to carry a JSON body; the method is not the cause.
- B. JSON bodies arrive in req.body after parsing, never in req.query, so this is false.
- C. A trailing slash does not affect whether the request body is parsed.
Tip: Always add express.json() for JSON POST requests in Express.
Question 5Medium
You call router.use(requireAuth) on an admin router mounted at /admin. Which requests run requireAuth?
- Every request the app receives
- Only requests that go through that router
- Only requests to the path
"/" - None until
app.use(requireAuth) is called too
Answer: B. Only requests that go through that router
Explanation
Router-level middleware is scoped to the router, which makes it a clean way to protect a whole group of routes.
Step-by-step solution
- router.use adds requireAuth to all routes under /admin.
- Only requests matching /admin paths trigger this middleware.
- Other routes like / or /home do not use this middleware.
- RequireAuth runs only for routes in the admin router group.
- Final answer: Only requests that go through that router.
Why the other options are wrong
- A. A router's middleware runs only for requests that pass through that router, not the whole app.
- C. Middleware on a router with no path runs for all requests into that router, not only the root path.
- D. It needs no app.use() call; the router.use() registration alone is enough for those requests.
Tip: Remember: Router middleware only affects routes under that router.
Question 6Medium
Which pattern reliably sends an error from an async handler to the error middleware in Express versions that do not handle promises?
- Return the error from the handler function
try/catch and call next(err) in the catch block- Throw it inside a
setTimeout callback - Call
res.end(err) with the error object so Express can log it
Answer: B. try/catch and call next(err) in the catch block
Explanation
Without promise support, a rejected promise from an async handler is never seen by Express, so the error must be passed explicitly with next(err).
Step-by-step solution
- Async handlers in Express without promise support do not automatically pass errors to middleware.
- Using
try/catch allows capturing errors from async code inside the handler. catch block calls next(err) to explicitly send the error to the error middleware.- This ensures the error is handled by the middleware even if it's thrown in an async function.
- Other methods like throwing in
setTimeout or calling res.end(err) do not reliably trigger middleware. - Calling
next(err) inside catch block correctly sends the error to the error handler.
Why the other options are wrong
- A. Returning an error from a handler does nothing; the error must be passed to next.
- C. A setTimeout callback runs outside the handler, so Express never sees a throw made there.
- D. res.end with an error object does not route it to error middleware; it just ends the response.
Tip: Always use try/catch with next(err) in async handlers for error handling in older Express versions.
Question 7Medium
A router has router.get('/users', h) and is mounted with app.use('/api', router). Which URL reaches h?
- GET /users
- GET /api
- GET /api/users
- GET /users/api
Answer: C. GET /api/users
Explanation
Paths inside a router are relative to its mount point, so the full path is the mount prefix plus the route path.
Step-by-step solution
- The router is mounted at /api.
- Its /users route becomes /api/users.
Why the other options are wrong
- A. Without the mount prefix it will not match.
- B. That would be the router root.
- D. Prefix comes first.
Tip: Prefix then route.
Question 8Medium
How does Express recognise a middleware as an error handler?
- Its name starts with "error"
- It is registered with
app.error() - It is the last
app.use() call in the file, after all the routes - It declares four parameters:
(err, req, res, next)
Answer: D. It declares four parameters: (err, req, res, next)
Explanation
Express checks the function's arity. Handlers with four parameters are skipped in normal flow and called when next(err) is invoked or a handler throws.
Step-by-step solution
- Express uses function parameter count to identify error handlers.
- A middleware with four parameters (err, req, res, next) is treated as an error handler.
- This allows the middleware to catch errors thrown in earlier routes or middleware.
- The fourth parameter 'next' is used to pass the error to the error-handling middleware.
- Thus, declaring four parameters makes it an error handler.
Why the other options are wrong
- A. The name of a function does not matter to Express; only the parameter count is checked.
- B. There is no app.error() method in Express; error handlers are registered with app.use().
- C. Position does not mark an error handler; it is the four-parameter signature that does.
Tip: Remember: Error handlers in Express have four parameters (err, req, res, next).
Question 9Hard
app.get('/users/:id', a) is registered before app.get('/users/me', b). Which handler handles GET /users/me?
- b, because exact matches take priority
- Both run, b first and then a
- Neither; Express reports a route conflict
- a, with req.params.id equal to "me"
Answer: D. a, with req.params.id equal to "me"
Explanation
Express tries routes in order and :id matches any segment, including "me". Register the specific route first.
Step-by-step solution
- Express checks routes in registration order.
- The pattern '/users/:id' is registered first and matches '/users/me', with id set to 'me'.
- So handler a runs and req.params.id is 'me'. The correct answer is D (a, with req.params.id equal to "me").
Why the other options are wrong
- A. Express does not prefer exact matches by order of specificity; the first match wins.
- B. Only the first matching route that calls next or responds is used here; b is not run first.
- C. No route conflict error is raised; the first matching route handles the request.
Tip: Put specific routes such as /users/me before parameter routes such as /users/:id.
Question 10Hard
In Express, what does calling next('route') inside a route callback do?
- Skips that route's remaining callbacks for the next route
- Restarts the whole middleware stack from the first
app.use() call - Sends a 404 Not Found response immediately
- Redirects the client to the URL named "route"
Answer: A. Skips that route's remaining callbacks for the next route
Explanation
next('route') bypasses the other callbacks of the current route and continues with the next matching route. It only works in app/router method handlers.
Step-by-step solution
- next('route') skips remaining callbacks in the current route.
- It moves to the next matching route defined in the router.
- This allows handling of requests by different routes sequentially.
- The request continues processing with the next route's middleware.
- The original route's further callbacks are not executed.
Why the other options are wrong
- B. It does not restart the whole stack from the first app.use(); it moves to the next route.
- C. It is not a 404; Express moves on to matching routes, and a 404 happens only if none match.
- D. It does not redirect; the string 'route' is a signal to Express, not a URL.
Tip: Use next('route') to handle requests with multiple routes sharing the same URL path.
Practise all 17 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →