繁体   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