1. Concept library
  2. Backend with Node.js
  3. Module 2: HTTP fundamentals

Concept

Headers and content types

Headers carry metadata: what format the body is in, how long to cache, who the caller is. Content-Type on both request and response is what lets each side parse the body at all.

What a session covers

The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.

  1. Explain what Content-Type lets each side do
  2. Set headers for JSON, caching and a custom request id
  3. Spot the JSON response sent without a Content-Type

In practice

A few lines that show Headers and content types doing its job.

JavaScript
app.post("/orders", async (req, reply) => {
  if (!req.headers["content-type"]?.startsWith("application/json")) {
    return reply.code(415).send({ error: "send JSON" });
  }
  const order = await orders.create(req.body);
  return reply
    .code(201)
    .header("location", "/orders/" + order.id)
    .header("cache-control", "no-store")
    .send(order);
});

The common mistake

Watch for

Sending JSON without Content-Type: application/json. Some clients then treat the body as text, and frameworks on the receiving end leave req.body undefined.

What to know first

Your roadmap puts these before Headers and content types, unless you already know them.

How it stays learned

Once a session shows you understand Headers and content types, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.

  1. Day 3First recall, while it is fresh
  2. Day 7Second, a week on
  3. Day 18Last, when it has settled

Where it leads

Topics that build on Headers and content types.

Previous in the module← Status codes