Skip to content
tezvyn:

Middleware

49 bites tagged Middleware — interview questions with model answers, and 60-second explainers.

Node.js & Express1 min read

How to authenticate WebSocket connections using JWTs?

Client sends JWT on connection, server validates via middleware, socket is attached to user. WebSocket auth patterns and middleware understanding.

React & Next.js1 min read

Session auth in Next.js with API routes and middleware

Login route sets a signed httpOnly cookie, middleware validates the session at the edge and redirects, server components read the session. Session flow on a hybrid framework.

React Native1 min read

Handling async API calls in Redux

Middleware intercepts dispatched actions, Thunk dispatches functions for simple flows, Saga uses generators for complex orchestration. understanding of async side effects in a synchronous store.

Python & FastAPI2 min read

Reading the full response body in middleware

Responses stream as multiple body messages and headers go first, so you cannot add a header after seeing the body; you must buffer all chunks, compute the hash, set the header, then resend. Understanding ASGI's streaming send model.

Python & FastAPI1 min read

Propagating a correlation ID without parameter passing

Middleware reads or generates the header, stores it in a contextvars.ContextVar, service code reads it anywhere, and logging filters inject it. Ambient request-scoped context in async code.

Node.js & Express2 min read

Unit testing Express auth middleware in isolation

Build fake req/res, use a spy/mock for next and res methods, assert next called on valid token and 401 sent on invalid. isolating and unit-testing middleware.

Node.js & Express1 min read

Validating request bodies with Express middleware

Run validation middleware before the handler, check email format and password length, return 400 with errors on failure, call next on success. separating validation from business logic via middleware.

Node.js & Express1 min read

Reusable schema validation middleware with Zod or Joi

Define a schema (email, password min 8, optional firstName), write a factory middleware that validates req.body, returns 400 with messages on failure, and assigns the parsed value on success. schema-driven validation as middleware.

Node.js & Express1 min read

Role-based access control middleware in Express

Authenticate first to set req.user, then a parameterized role-check middleware that compares req.user.role and returns 403 if it fails, applied to protected routes. layered authorization design.

Node.js & Express1 min read

Securing Express with Passport local strategy

Configure LocalStrategy with a verify callback, call passport.authenticate as route middleware, and set up serializeUser/deserializeUser for sessions. practical Passport.js wiring.

Node.js & Express1 min read

Mongoose pre('save') hooks for password hashing

Pre('save') runs before persistence; use it to hash the password, guarding with isModified, calling next() or returning. lifecycle hooks on documents.

Node.js & Express1 min read

Conditionally apply middleware by request property

Use express.text({ type: 'application/xml' }) or a guard wrapper that checks req.is() then calls the parser or next(). knowing helpers and the type option exist.

Node.js & Express1 min read

Middleware execution order and sharing data via req

Middleware runs top-down in registration order; each calls next(); attach data like req.user that later handlers read. the sequential pipeline and shared req object. assuming parallel execution or using globals to pass state.

Node.js & Express1 min read

Express error-handling middleware signature

(err, req, res, next) with err first, identified by arity, placed last after all routes. recognizing the four-argument signature. omitting the err parameter or registering it before the routes it should catch.

Node.js & Express1 min read

Application-level vs router-level middleware

App.use() binds to the app and runs everywhere; router.use() binds to a Router and runs only for that router's routes. scoping of middleware. claiming they are interchangeable or that scope does not matter.

Node.js & Express1 min read

Write a request-logging middleware in Express

Read req.method, log with a timestamp, then call next() to continue. the (req, res, next) signature and calling next(). forgetting next() so the request hangs, or ending the response prematurely.

Node.js & Express1 min read

Parse JSON and URL-encoded bodies in Express

Express.json() for JSON, express.urlencoded() for form data, both registered via app.use(). knowledge of built-in body parsers. reaching for the deprecated body-parser package or forgetting extended option.

Node.js & Express1 min read

What is Express middleware

Middleware are functions in a chain with req, res, next, they inspect or modify request and response, then call next to continue or send a response to end. the core Express request pipeline.

Node.js & Express1 min read

Centralized error-handling middleware

Error middleware takes four args err, req, res, next, is defined last, and runs when next(err) is called or sync errors throw. Express error flow. omitting the err parameter so Express treats it as normal middleware.

Node.js & Express1 min read

Custom API key auth middleware

Read the header from req, on missing or invalid send res.status(401) and return, on valid call next, mount before protected routes. writing middleware with the req, res, next contract.

Node.js & Express1 min read

Parsing JSON bodies with express.json

Register express.json via app.use so it reads the request stream and populates req.body before handlers run, place it before routes. why req.body needs a body parser. expecting req.body to work without any parser.

Node.js & Express1 min read

Serving static files in Express

App.use with express.static pointing at the public directory, files served relative to that root, often combined with an absolute path. knowing the built-in static middleware.

Node.js & Express1 min read

Handling async errors in Express middleware

Await inside try/catch and call next(err) on failure, or wrap the handler in an async error adapter; never let a rejected Promise go uncaught. routing async errors into Express.

Go & Rust1 min read

Logging middleware wrapping an http.Handler in Go

Middleware has signature func(http.Handler) http.Handler, records start time, calls next.ServeHTTP, then logs method, URL, and elapsed duration; chaining works because the wrapper is itself a… the http.Handler middleware pattern.

Get Middleware bites daily.

Five a day, five minutes, offline. With quizzes so it sticks.

Open testing — you’ll join as an early tester.