簡體   English   中英

有沒有一種方法可以檢查用戶是否使用express.router中間件登錄?

[英]Is there a way to check if user is logged in using express.Router middleware?

如何使用router.route將isLoggedIn作為獲取請求的條件插入?

const controller = require('./controller');
const Router = require('express').Router;
const router = new Router();

function isLoggedIn(req, res, next) {
  if (req.isAuthenticated())
    return next();
  res.redirect('/');
}

router.route('/')
  .get((...args) => controller.find(...args))

我認為...args是我嘗試過的(req,res,next)

router.route('/')
      .get(isLoggedIn(...args) => controller.find(...args))

但是我明白了

.get((isLoggedIn(...args)) => controller.find(...args))
                ^
SyntaxError: Unexpected token (

文檔說,您可以將多個處理程序分配給一條路由。 像這樣:

app.use('/user/:id', function (req, res, next) {
  console.log('Request URL:', req.originalUrl)
  next()
}, function (req, res, next) {
  console.log('Request Type:', req.method)
  next()
})

資源

在您的情況下,代碼如下所示

router.get('/', isLoggedIn, controller.find);

暫無
暫無

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

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