簡體   English   中英

如何檢查每個請求在Express JS中都有cookie

[英]how to check every request have the cookie in express js

我正在使用react和node構建一個原始應用程序,因此我需要檢查每個請求是否存在cookie。

app.all("*", (req,res) => {
 // cookie doesn't exist redirect to login
 if(cookieExist(req.headers.cookie)){
   // how to pass to the next layer ? load the routes below code etc..
  next(); 
 }else{
    res.redirect("/login")
  }
})

const routes = require("./routes/route");

app.use(bodyParser.json());
app.use(cors());

app.use("/apiServices", apiRoutes)

我在這里所缺少的,得到未定義的next()。

定義next作為參數

app.all("*", (req,res, next) => {
 // cookie doesn't exist redirect to login
 if(cookieExist(req.headers.cookie)){
   // how to pass to the next layer ? load the routes below code etc..
  next(); 
 }else{
    res.redirect("/login")
  }
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM