json 响应未压缩为 express
json response is not compressed in express
我使用压缩库来压缩我所有的快递回复。不幸的是,none 的回复似乎被压缩了。然而,我在客户端调用 header 中指定了 accept-encoding: "gzip"。这是代码:
正面:
export const api = ax.create({
baseURL: "http://localhost:8000",
timeout: 1000,
withCredentials: true,
headers: {
'Accept-Encoding': 'gzip',
}
});
const contact = () => {
api.get("/contact")
.then(res=> console.log("contact response", res))
.catch(err=> console.log("contact err", err))
}
返回:
const app = require("express")();
const cors = require("cors");
const compression = require("compression")
app.use(cors({ credentials: true, origin: ["http://localhost:3000"] }));
app.use(compression());
app.get('/contact', (req, res) => {
res.status(200).json({hello: "world"})
})
在 chrome 网络选项卡中,我没有 content-encoding: gzip。如何解决这个问题?
压缩的默认阈值是 1kb (Source)。如果您的请求小于 1kb,则不会压缩。
如果需要,您可以修改阈值并将其设置为 0 字节。
app.use(compression({
threshold: 0
}));
我使用压缩库来压缩我所有的快递回复。不幸的是,none 的回复似乎被压缩了。然而,我在客户端调用 header 中指定了 accept-encoding: "gzip"。这是代码:
正面:
export const api = ax.create({
baseURL: "http://localhost:8000",
timeout: 1000,
withCredentials: true,
headers: {
'Accept-Encoding': 'gzip',
}
});
const contact = () => {
api.get("/contact")
.then(res=> console.log("contact response", res))
.catch(err=> console.log("contact err", err))
}
返回:
const app = require("express")();
const cors = require("cors");
const compression = require("compression")
app.use(cors({ credentials: true, origin: ["http://localhost:3000"] }));
app.use(compression());
app.get('/contact', (req, res) => {
res.status(200).json({hello: "world"})
})
在 chrome 网络选项卡中,我没有 content-encoding: gzip。如何解决这个问题?
压缩的默认阈值是 1kb (Source)。如果您的请求小于 1kb,则不会压缩。
如果需要,您可以修改阈值并将其设置为 0 字节。
app.use(compression({
threshold: 0
}));