skip to content

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 pageshow

explore

questions

page 2 of 2

In Express 5, how do res.format, res.redirect and res.render let one route answer browsers with HTML and API clients with JSON?

level: middleimportance: should knowfreq 30%

basics

~20 s

res.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.

open as a page

In Express 5, what do the limit, type, strict and verify options of express.json control, and how does each one fail a request?

level: middleimportance: should knowfreq 48%

basics

~20 s

limit 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.

open as a page

In Express 5, how do you send 201 with a Location header on create and 204 on delete, and what do response helpers add?

level: middleimportance: should knowfreq 50%

basics

~10 s

Chain 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.

open as a page

In Express 5, what do app.param callbacks and next('route') each do, and when does a param callback run during a request?

level: middleimportance: should knowfreq 30%

basics

~20 s

app.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.

open as a page

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?

level: middleimportance: should knowfreq 35%

basics

~20 s

Express 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.

open as a page

In Express 5, why is req.params.orgId undefined inside an express.Router mounted at /orgs/:orgId/users, and what does mergeParams change?

level: middleimportance: should knowfreq 48%

basics

~20 s

Each 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.

open as a page

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?

level: middleimportance: should knowfreq 42%

basics

~20 s

A 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.

open as a page

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?

level: middleimportance: should knowfreq 45%

basics

~20 s

Assert 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.

open as a page

After adding Clerk's clerkMiddleware() to an Express API, requests with no session still reach your handlers. Why, and how should you protect routes?

level: seniorimportance: should knowfreq 25%

basics

~20 s

clerkMiddleware() 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.

open as a page

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?

level: seniorimportance: should knowfreq 35%

basics

~20 s

passport.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.

open as a page

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?

level: seniorimportance: should knowfreq 30%

basics

~20 s

req.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.

open as a page

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?

level: seniorimportance: should knowfreq 42%

basics

~20 s

Express 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).

open as a page

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?

level: seniorimportance: should knowfreq 38%

basics

~20 s

passport-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.

open as a page

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?

level: seniorimportance: should knowfreq 46%

basics

~20 s

The 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.

open as a page

After a logging error middleware is added to an Express 5 app, failing requests return 404 'Cannot GET' instead of 500; what went wrong?

level: seniorimportance: should knowfreq 40%

basics

~20 s

The 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'.

open as a page

In Express, why does auth middleware added with authRouter.use() also reject the public routes of a second router mounted on the same /users path?

level: seniorimportance: should knowfreq 30%

basics

~20 s

router.use(fn) without a path runs for every request that enters that router, and a router mounted with app.use('/users', ...) is entered by every /users request. Mounted first, authRouter rejects GET /users/42 before the second router's public route is ever tried.

open as a page

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?

level: seniorimportance: should knowfreq 30%

basics

~20 s

In 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.

open as a page

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?

level: seniorimportance: should knowfreq 40%

basics

~10 s

Express 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.

open as a page

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?

level: seniorimportance: should knowfreq 45%

basics

~20 s

In 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).

open as a page

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?

level: seniorimportance: should knowfreq 40%

basics

~20 s

Leave 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.

open as a page

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?

level: seniorimportance: should knowfreq 45%

basics

~20 s

Express 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.

open as a page

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?

level: seniorimportance: should knowfreq 38%

basics

~20 s

Supertest'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.

open as a page

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?

level: principalimportance: should knowfreq 35%

basics

~20 s

Choose 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.

open as a page

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?

level: principalimportance: should knowfreq 28%

basics

~20 s

Express 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.

open as a page

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?

level: principalimportance: should knowfreq 25%

basics

~20 s

Mandate 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.

open as a page

When you mount better-auth in an Express 5 app with toNodeHandler, where do users and sessions live, and what must exist first?

level: middleimportance: nice to knowfreq 22%

basics

~20 s

better-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)).

open as a page

In Express, how does next('router') let middleware bail out of a router mounted at /admin, and what runs after it?

level: middleimportance: nice to knowfreq 28%

basics

~20 s

Calling 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.

open as a page

showing 31–58 of 58