difference between 'done' and 'next' in node.js callbacks

前端 未结 4 1447
情歌与酒
情歌与酒 2020-12-24 03:27

in the passport [configure authentication] documentation, it has a rather scary-looking function that uses the mysterious function \"done.\'

passport.use(new         


        
4条回答
  •  無奈伤痛
    2020-12-24 03:55

    Let's back up because I think you may have some confusion.

    Express is a web application framework. It's responsible for directing users to resources, in a very broad sense.

    Passport is a authentication framework. It's responsible for making sure that users are allowed to access said resources.

    In both frameworks there is a idea of middleware. Middleware is basically generalized control flow. For example, in some Express framework you could say:

    1. Make sure parameter x is valid when requesting route /user/:x

      • if valid, then next() --> which means go to next middleware function()
    2. Make sure the user has a session, etc

    3. And when all middleware has been executed, then we execute the application

    For example,

    router.get('/', function(req, res) { // when the '/' route is requested
        res.render('index', { title: 'Express' }); // send index.html
    });
    

    In Passport, they also use the idea of middleware, however, instead of next(), they use done() and it's a little more complex. See this page for more info
    http://toon.io/understanding-passportjs-authentication-flow/

提交回复
热议问题