sg.*_*.cc 1 javascript node.js express passport.js
我正在学习一个关于使用 Node.js 包 Passport (链接)进行用户身份验证的优秀教程,但我遇到了一段我真的不理解的代码:
app.get('/profile', isLoggedIn, function(req, res) {
res.render('profile.ejs', {
user : req.user // get the user out of session and pass to template
});
});
Run Code Online (Sandbox Code Playgroud)
我的问题是参数isLoggedIn。我查看了官方网站,并进行了一些谷歌搜索,但没有任何地方说您可以将三个参数传递给app.get. 我只见过两个。第三个(我认为是可选的)参数是什么?
我不是在问它isLoggedIn本身,而是在问它是我以前从未见过的第三个参数这一事实app.get()。
它位于 Express 文档中:http://expressjs.com/en/5x/api.html#app.get
语法是:
app.get(path, callback [, callback ...])
即
app.get(path, ...callback)
该语法包括将路径作为第一个参数,然后是您想要的任意数量的中间件(可以访问请求和响应)回调函数。并不局限于一种。它们是异步的,并通过调用 next() 参数链接在一起。
function callbackOne(req, res, next) {
//some code
next();
}
function callbackTwo(req, res, next) {
//some code
res.render();
}
app.get(path, callbackOne, callbackTwo)
Run Code Online (Sandbox Code Playgroud)
因此,在您的情况下, isLoggedIn 参数只是另一个中间件函数,如果用户登录以执行第三个参数,该函数最终会调用 next() 。
| 归档时间: |
|
| 查看次数: |
3313 次 |
| 最近记录: |