我使用 Node js 实现了简单的代理服务器。 因此,代理一旦收到请求就会发送早期提示响应,它对于 HTTP/1.1 请求效果很好,但是当我尝试使用 HTTP/2 请求时,它不会发送早期提示。 我需要处理 HTTP/2 早期提示,因为大多数浏览器只接受 HTTP/2 或以上早期提示,而不接受 HTTP/1.1 早期提示。 请帮我解决这个问题。
const http2 = require("http2");
const fs = require("fs");
const proxy = require("http2-proxy");
const options = {
key: fs.readFileSync("./key.pem"),
cert: fs.readFileSync("./cert.pem"),
passphrase: "password",
allowHTTP1: true,
};
const server = http2.createSecureServer(options, (req, res) => {
console.log("Hello", req.httpVersion);
res.writeEarlyHints(
{
link: ["</_next/static/css/11031e50f5a9bdb7.css>; rel=preload; as=style"],
},
() => {
console.log("Early Hints sent");
}
);
proxy.web(req, res, {
hostname: "mantine.dev",
protocol: "https",
});
});
server.listen(5000);
我尝试记录早期提示,并注意到 writeEarlyHints 的回调函数在仅 HTTP/2 请求的情况下不会执行。
请检查 Node API:https://nodejs.org/api/http2.html。
response.writeEarlyHints(hints)#
hints <Object>
Http2 服务器没有回调参数,它仅与 Http1 服务器响应相关。