[英]Is there a way to check if user is logged in using express.Router middleware?
How can I insert isLoggedIn
as a condition to the get request using router.route? 如何使用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))
I assume that the ...args
are (req, res, next) I tried 我认为...args
是我尝试过的(req,res,next)
router.route('/')
.get(isLoggedIn(...args) => controller.find(...args))
But I get 但是我明白了
.get((isLoggedIn(...args)) => controller.find(...args))
^
SyntaxError: Unexpected token (
The docs say, that you can assign multiple handlers to one route. 文档说,您可以将多个处理程序分配给一条路由。 Like this: 像这样:
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()
})
In your case the coding looks like the following 在您的情况下,代码如下所示
router.get('/', isLoggedIn, controller.find);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.