[英]Getting “Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client” when trying to redirect
[英]“Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client” when trying to redirect
如果有人輸入正確的密鑰(1234),我試圖設置一個cookie,但是當他們鍵入正確的密鑰時,它會吐出一個錯誤
錯誤[ERR_HTTP_HEADERS_SENT]:將標頭發送到客戶端后無法設置標頭
idk我該怎么做,我試圖在設置cookie之前進行重定向,甚至只是在重定向后記錄日志,但錯誤並沒有消失!
這是我的代碼:鍵未在本部分中定義
app.get("/", function(req,res) {
if (req.cookies.userSecret == key) {
res.sendFile(__dirname + "/index.html");
} else {
res.sendFile(__dirname + "/login.html")
io.on("connection", function(socket) {
socket.on("login", function(data){
if (data == key) {
res.redirect("/1799fd8e-78aa-4bc0-9692-011a81c07248")
return false;
} else {
io.emit("log", "Wrong key! try again")
return false;
}
})
})
}
});
app.get("/1799fd8e-78aa-4bc0-9692-011a81c07248", function(req,res) {
console.log(key)
res.sendFile(__dirname + "/index.html")
})
發送請求后,您將無法重定向用戶。 您必須從客戶端重定向用戶。 就像您在其他部分中所做的一樣,您將向用戶發出一個包含URL的事件,該URL重定向到客戶端,並在客戶端接收此事件並使用window.location
更改URL。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.