Lesson 9.5

Content negotiation

Parse the client's Accept header with q-values and media-range specificity, pick the best representation the server can offer, and wire it into res.format with the Vary header that keeps caches from serving the wrong bytes.

30 minsrc/negotiate.js, src/response.js, examples/demo.js
  • Parse Accept into a preference-sorted list using q-values and specificity.
  • Pick the best offered type for a client, or null when none is acceptable.
  • Build res.format with a 406 fallback and a deduplicated Vary header set to Accept.
Locked

This chapter is part of the framework capstone. Chapters 1 and 2 are free to build - unlock the rest to keep going.

Unlock the depth