Backend Architecture
Middleware and DI: multiple-choice review
Six questions that cut across the whole unit. Each mirrors a judgement you make on a real backend — which axis a problem lives on, what the contract forbids, what lifetime an object should have — not a definition to recite.
Confirm you can place a problem on the right axis (request vs wiring), reason about middleware ordering and the response contract, choose an injection scope, and read a container failure — the synthesis the six lessons built toward.
A handler is slow on every single endpoint, and separately a billing class is impossible to unit-test without hitting the real Stripe API. On which axis does each problem live?
An Express middleware sends res.json(...) on a validation failure and then also calls next(). Under load it intermittently throws 'Cannot set headers after they are sent'. What rule was broken?
A central Express error handler written as (err, req, res) never receives errors even though earlier code calls next(err). Why, and what is the fix?
Intermittently, user A sees user B's account name. Auth is correct. A service registered as a singleton stores this.currentUser per request. What happened, and what is the cleanest fix?
A behavior-preserving refactor splits one repo.save() into two saves inside a transaction. Final state is identical, yet forty tests go red. What does this reveal?
An app boots in dev but crashes in prod with 'Nest can't resolve dependencies of UserService'. AuthService now imports UserService, which already imported AuthService. How do you read this, and what is the real fix?
The unit’s through-line is one habit: name the axis, then apply its discipline. The request axis (middleware) demands one ending per middleware and a deliberate order; error middleware needs four parameters. The wiring axis (DI) means classes declare needs and a composition root supplies them — default to stateless singletons, escalate scope only when forced, and read test pain as a design signal (mock the boundaries you don’t own, fake what you do). A container is that composition root mechanized: it topologically sorts the graph, refuses cycles, and should fail fast at boot. Every question here resolves back to those two axes and the cost each carries.