我有一個非常簡單的 express.js 服務器,
app.get("/:var", (req, res) => {
console.log(req.params.var);
res.redirect(301, "/");
});
現在在我的瀏覽器上,如果我一個接一個地訪問以下網址
localhost:127/abc
localhost:127/abc
localhost:127/xyz
localhost:127/abc
localhost:127/xyz
localhost:127/xyz
localhost:127/abc
我在服務器的終端中得到以下輸出
abc
xyz
出于某種原因,如果我洗掉res.redirect(301, "/"),一切都按預期作業,我得到以下輸出
abc
abc
xyz
abc
xyz
xyz
abc
即使我重新啟動服務器,我也不能使用我在重新啟動之前使用過的值。只有當我從瀏覽器中洗掉快取時,我才能再次使用該值一次。
有人可以解釋發生了什么嗎?我需要保留重定向并在重定向之前處理來自 url 的資料,并且希望客戶端每次想要使用該站點時都不會清除瀏覽器快取。
uj5u.com熱心網友回復:
這里說:A 301 response is cacheable by default; i.e., unless otherwise indicated by the method definition or explicit cache controls- 參考:https ://httpwg.org/specs/rfc7231.html#status.301
所以你看到的行為是正確的,你必須清除瀏覽器中的快取才能看到新的回應。
上面的陳述句也提出了解決方案,您可以設定快取標頭來告訴瀏覽器不要快取回應。就像是:
res.set("cache-control","no-store")
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/462331.html
標籤:javascript 节点.js 表示 服务器 路线
