Routing, middleware, error handling and structuring an Express API.
A home-page handler loads several widgets with Promise.allSettled and
must degrade gracefully. Write entryPoint(results, required) that
builds the response from the settled results.
In Express 5, when an async handler's promise rejects, the rejection
reason is passed to next(err). A falsy reason is replaced by
new Error("Rejected promise"). Your error middleware then picks the
status the same way Express's default handler does. Write
A route app.get("/bookings/:id", async (req, res) => { const b = await db.findBooking(req.params.id); res.json(b); }) hits a database timeout,
so rejects. What happens to the request in Express 5, and how
was the same code dangerous in Express 4?
findBookingA teammate writes app.get("/health", async (req, res) => { const ok = await db.ping(); return { status: ok ? "up" : "down" }; }), expecting
Express to send the returned object as JSON like some other frameworks
do. What actually happens, and what does Express 5 do with a handler's
After a redirect ("Booking saved", "Card declined"), Express apps often
show one-time "flash" messages stored in the session, in the style of
connect-flash. Simulate the req.flash API over one session.
Express does not parse the Cookie request header for you; middleware
such as cookie-parser or express-session does it with the cookie
package. Write entryPoint(header) that parses a raw Cookie header
and returns an object mapping cookie names to values.