为什么响应 304 没有 Content-Type header?

Why response 304 doesn't have Content-Type header?

我一直在玩 express.js 尝试 return 简单 json object 并注意到即使我明确设置 Content-Type header 为 application/json 它仅在状态代码为 200 时的第一个响应中可见。每个随后的 304 响应都不会 Content-Type header.

我的代码示例:

app.get('/user', function (req, res) {
    res.set('Content-Type', 'application/json');

    res.send([
        { user: "john", email: "john@example.com"},
        { user: "marry", email: "marry@example.com"},
        { user: "dan", email: "dan@example.com"}
    ]);
});

这是什么原因?

304 Not Modified 表示请求包含 conditional header 要求服务器响应资源内容 仅当 资源已被修改.

由于未返回任何内容,因此未发送 Content-Type header。这是 the recommended behavior304 Not Modified HTTP 回复。

来自 RFC 7232 §4.1 :

The server generating a 304 response MUST generate any of the
following header fields that would have been sent in a 200 (OK)
response to the same request: Cache-Control, Content-Location, Date,
ETag, Expires, and Vary.

Since the goal of a 304 response is to minimize information transfer when the recipient already has one or more cached representations, a sender SHOULD NOT generate representation metadata other than the above listed fields unless said metadata exists for the purpose of
guiding cache updates (e.g., Last-Modified might be useful if the
response does not have an ETag field).

我对 express.js 一无所知,但我会调查正在执行的缓存类型。