Express.js
The minimal Node framework: an ordered middleware stack, routers and thin request and response helpers, now on version 5. Interviewers use it because whatever it leaves to you, you must explain.
on this pageshowhide
explore
- Routes and Path Syntax5 questions
- Middleware Pipeline6 questions
- Request & Response5 questions
- Four-Argument Error Handlers5 questions
- REST API Patterns6 questions
- Production Hardening & Deployment6 questions
- Identity & Permissions20 questions
- Strategies & Verify Callbacks5 questions
- Session vs Token Mode5 questions
- Session Store Wiring5 questions
- Auth Library Landscape5 questions
- HTTP Route Testing5 questions
questions
page 2 of 2In Express 5, how do res.format, res.redirect and res.render let one route answer browsers with HTML and API clients with JSON?
basics
~20 sres.format picks a callback by the Accept header, so an html branch can redirect or render while a json branch calls res.json. res.redirect defaults to 302 and takes a status first; res.render needs a view engine and sends 200 HTML.
In Express 5, what do the limit, type, strict and verify options of express.json control, and how does each one fail a request?
basics
~20 slimit caps body size (100kb default, 413 when exceeded), type picks which Content-Type is parsed (others are skipped), strict accepts only objects and arrays (else 400), and verify inspects the raw Buffer, failing with 403 when it throws.
In Express 5, how do you send 201 with a Location header on create and 204 on delete, and what do response helpers add?
basics
~10 sChain the status before the body: res.status(201).location(url).json(order) on create, res.sendStatus(204) or res.status(204).end() on delete. A helper fixes each pairing once, so no route forgets the Location header or sends a body with 204.
In Express 5, what do app.param callbacks and next('route') each do, and when does a param callback run during a request?
basics
~20 sapp.param(name, callback) runs once per request, before the handlers of any matching route that declares that parameter, which suits loading a record by id; next('route') skips the remaining callbacks of the current route and resumes matching at the next route.
In Express 5, how do you write a named wildcard, an optional segment and a digits-only parameter in a route path without inline regex?
basics
~20 sExpress 5 parses string paths with path-to-regexp v8: a wildcard needs a name such as /*splat and captures an array, braces make a part optional as in /:file{.:ext}, and a digits-only rule needs a RegExp route.
In Express 5, why is req.params.orgId undefined inside an express.Router mounted at /orgs/:orgId/users, and what does mergeParams change?
basics
~20 sEach express.Router fills req.params only from its own route paths, so a parameter in the parent's mount path is hidden from the child. express.Router({ mergeParams: true }) merges the parent's params in, and the child wins a conflict.
In a Supertest suite for an Express app, why can a test that never awaits or returns its request(app) chain pass, and what does it leave behind?
basics
~20 sA Supertest chain sends nothing until .then() (which await calls) or .end() runs, so a test that only builds it returns at once and is reported as passing. The ephemeral server Supertest already started is never closed, so the run can hang.
How do you test an Express 5 error path with Supertest so the assertion proves your four-argument error handler answered, not the default handler?
basics
~20 sAssert more than the status: Express 5's default error handler also takes the status from err.status, but it answers text/html with the stack outside production. Expect your JSON content type and body shape, so a test only passes when the custom handler ran.
After adding Clerk's clerkMiddleware() to an Express API, requests with no session still reach your handlers. Why, and how should you protect routes?
basics
~20 sclerkMiddleware() only reads the session token and attaches the auth state to the request; it calls next() for signed-out requests too. Protect routes with your own middleware that checks getAuth(req).userId and returns 401 when it is null.
An Express API route guarded by passport.authenticate('jwt') answers 500 'Login sessions require session support' for valid tokens only; why, and what is the fix?
basics
~20 spassport.authenticate defaults session to true, so once passport-jwt accepts the token Passport calls req.login, which needs req.session; with no session middleware it errors and the error becomes a 500. Pass { session: false } on the API route.
With Passport in an Express app, why does a dashboard sign-in that calls req.login(user, { session: false }) leave the user anonymous on the next request?
basics
~20 sreq.login always sets req.user, but only with session left at its default true does it regenerate the session, run serializeUser and save req.session.passport.user; under session: false it serializes nothing, so the login lasts one request.
With cookie-session in an Express app, what does its Keygrip signature protect, and what does it leave exposed?
basics
~20 scookie-session stores the whole session as base64 JSON in the cookie and signs it with Keygrip, so tampering is detected; but anyone holding the cookie can read it, replay it until it expires, and the server cannot revoke it.
In an Express 5 app, an async passport-local verify callback throws during a database outage and the whole Node process exits; why doesn't Express 5 catch it, and what is the fix?
basics
~20 sExpress 5 forwards only rejections of a promise the middleware returns. passport.authenticate returns nothing and passport-local catches only synchronous throws, so the rejection goes unhandled and Node's default throw mode exits. Catch inside verify and call done(err).
With Passport in an Express app, how does passport-openidconnect turn a provider callback into a user, and why does passport-google-oauth20 never read the id_token?
basics
~20 spassport-openidconnect exchanges the code, checks the ID token's issuer, audience and expiry, builds the profile from its claims and calls verify(issuer, profile, cb). passport-google-oauth20, an OAuth 2.0 strategy, fetches the profile from userinfo with the access token.
An Express 5 CSV export fails midway through streaming and the logs show ERR_HTTP_HEADERS_SENT instead of the real cause; why, and what should the error handler do?
basics
~20 sThe error handler tries res.status(500).json() after the 200 headers went out; res.json throws ERR_HTTP_HEADERS_SENT, which replaces the real error. Check res.headersSent first and return next(err): Express's default handler logs the original and destroys the socket.
After a logging error middleware is added to an Express 5 app, failing requests return 404 'Cannot GET' instead of 500; what went wrong?
basics
~20 sThe logging handler calls next() instead of next(err). In Express, next() with no argument clears the pending error, so dispatch resumes with ordinary middleware and routes after it; nothing matches, and Express's built-in handler answers 404 'Cannot GET /path'.
After an upgrade to Express 5, express.static serving a built frontend returns 404 for /.well-known/ files; why, and how should its cache options be set?
basics
~20 sIn Express 5, express.static's dotfiles default 'ignore' also covers hidden directories in the path, so /.well-known/ is treated as missing and ends in 404. Serve that directory from its own mount, and set maxAge with immutable only for fingerprinted assets.
After an Express 4 to 5 upgrade, a search route's ?filter[status]=open stops filtering and middleware that rewrote req.query breaks; what changed?
basics
~10 sExpress 5 changed the default 'query parser' from 'extended' to 'simple', which keeps filter[status] as a flat key, and made req.query a getter that re-parses the URL, so assignments to it throw or vanish.
After upgrading to Express 5, handlers calling res.send(404), res.json(user, 201) and res.redirect('/login', 301) misbehave; what does each now do, and what replaces it?
basics
~20 sIn Express 5 res.send(404) answers 200 with the JSON body 404, res.json(user, 201) answers 200 because the status argument is ignored, and res.redirect('/login', 301) throws a TypeError, giving a 500. Use res.sendStatus, res.status(code).json and res.redirect(status, url).
After an Express 5 upgrade, a validation middleware running req.query = schema.parse(req.query) throws a TypeError; where should the validated query live, and what must its schema expect?
basics
~20 sLeave req.query as the raw parse, since Express 5 made it read-only, and have the middleware write the validated result to a property it owns, such as req.valid.query, with a schema that coerces the strings the query string arrives as.
After a catalogue API moves from Express 4 to 5, app.all('*') throws a TypeError at startup and an optional :format? route breaks; what changed, and how do you fix each route?
basics
~20 sExpress 5 parses route strings with path-to-regexp v8, which throws on bare *, ? and inline regex when the route is registered. Name the wildcard (/{*splat} includes the root), use braces for optional parts, then fix handlers reading the new captures.
With Supertest, how do you test a login-protected Express route, and why can a shared request.agent make an anonymous-access test pass or fail depending on test order?
basics
~20 sSupertest's request(app) keeps no cookies between calls, while request.agent(app) stores each Set-Cookie and sends it on later requests. An agent shared across tests carries one test's login into the next, so an anonymous-access check depends on order.
For a startup's new Express API, how would you choose between hand-wired Passport, Auth.js, better-auth and Clerk, and what does leaving each one cost?
basics
~20 sChoose by who holds users and what you must build: Passport only authenticates requests, Auth.js and better-auth bring endpoints with data in your database, Clerk hosts everything. Leaving the first three is a code rewrite; leaving Clerk also migrates users.
As the lead for many Express 5 services, which Express defaults and ecosystem middleware would you make mandatory in a shared bootstrap, and what does that cost?
basics
~20 sExpress defaults to x-powered-by on, env 'development' without NODE_ENV, trust proxy false and no security middleware. A shared bootstrap fixes those and mounts helmet, cors where needed, express-rate-limit and a health route; TLS and bulk compression usually stay at the proxy.
As the lead standardising Express 5 services, what house layout would you mandate for resource routers, validation middleware and response helpers, and what does it cost?
basics
~20 sMandate one router module per resource, a fixed app.js order, one shared validation factory that stores parsed input in one named place, and thin response helpers; the cost is a shared package every service must keep upgraded.
When you mount better-auth in an Express 5 app with toNodeHandler, where do users and sessions live, and what must exist first?
basics
~20 sbetter-auth keeps users, sessions, linked accounts and verification tokens in your own database, in user, session, account and verification tables it defines; those tables must exist first, and the handler is mounted with app.all('/api/auth/*splat', toNodeHandler(auth)).
In Express, how does next('router') let middleware bail out of a router mounted at /admin, and what runs after it?
basics
~20 sCalling next('router') inside an Express router skips every remaining layer of that router and hands the request back to the parent with no error. The parent then runs its next matching layer, such as a fallback mounted after the router that answers 401.
showing 31–58 of 58