[英]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.