Advanced HTTP in Express.js
Advanced HTTP is the seventh chapter of the free Express.js tutorial. It covers answering in the right format, working with other services, and pushing live updates.
What each lesson covers:
- Redirects, Content Negotiation and Proxies: Accept headers and res.format, redirect status codes, safe redirect URLs, reverse proxies and the trust proxy setting.
- Calling APIs, Background Jobs and Real-Time: Calling other APIs with fetch, timeouts, retries and idempotency keys, caching, background jobs with 202 Accepted, Server-Sent Events and WebSockets.
The first lesson covers what a server does beyond answering one request. Your Express app calls another API with fetch, sets timeouts and retries safely, and caches answers it has already fetched. It runs slow work as background jobs, and sends live updates to the browser with Server-Sent Events. The second lesson controls the response itself: content negotiation with res.format, redirects and how to build them safely, and running Express behind a reverse proxy without trusting headers it should not.
The labs let you send any header, and include pretend weather, payment and email services that behave like real ones.
Lessons in this section
- Redirects, Content Negotiation and Proxies
Content negotiation with Accept headers, req.accepts and res.format, 406, redirect status codes, res.redirect and res.location, building and checking redirect URLs, open redirects, reverse proxies, trust proxy and forwarded headers.
- Answer in the format and language a client asks for
- Choose the right redirect status
- Build safe redirect URLs
- Prevent open redirects
- Calling APIs, Background Jobs and Real-Time
Calling other APIs with fetch, checking response.ok, timeouts, retries with backoff, idempotency keys, caching, background jobs with 202 Accepted, fire-and-forget work, Server-Sent Events and WebSockets.
- Call other APIs with fetch and check the answer
- Add timeouts, retries and idempotency keys
- Cache slow answers
- Run slow work as a background job