【问题标题】:how to check every request have the cookie in express js如何检查每个请求在express js中都有cookie
【发布时间】:2018-11-28 11:40:01
【问题描述】:

我正在使用 react 和 node 构建一个 crud 应用程序,所以我需要检查每个请求是否存在 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()。

【问题讨论】:

标签: javascript node.js express


【解决方案1】:

将下一个定义为参数

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")
  }
})

【讨论】:

    猜你喜欢
    • 2021-02-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-26
    • 1970-01-01
    • 1970-01-01
    • 2016-05-18
    • 2021-04-21
    • 2018-05-23
    相关资源
    最近更新 更多