Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

return next() in nodejs confusion

https://github.com/hwz/chirp/blob/master/module-5/completed/routes/api.js

function isAuthenticated (req, res, next) {
    // if user is authenticated in the session, call the next() to call the next request handler 
    // Passport adds this method to request object. A middleware is allowed to add properties to
    // request and response objects

    //allow all get request methods
    if(req.method === "GET"){
        return next();
    }
    if (req.isAuthenticated()){
        return next();
    }

    // if the user is not authenticated then redirect him to the login page
    return res.redirect('/#login');
};

Why does the author do return next() instead of next()? I know next() is to let the flow jump to next middleware or function, but why it needs a return for next() above?

like image 365
Nichole A. Miler Avatar asked Nov 10 '15 12:11

Nichole A. Miler


People also ask

What is return next in node JS?

In this is article we will see when to use next() and return next() in NodeJS. Features: next() : It will run or execute the code after all the middleware function is finished. return next() : By using return next it will jump out the callback immediately and the code below return next() will be unreachable.

What does next () do in Express JS?

The next() function is a function in the Express router that, when invoked, executes the next middleware in the middleware stack. If the current middleware function does not end the request-response cycle, it must call next() to pass control to the next middleware function. Otherwise, the request will be left hanging.

What is next () middleware?

Next. js' middleware allows you to create functions that execute after a user's request is made and before the request is completed — in the middle of the two processes. This enables you to process a user's request and then modify the response by rewriting, redirecting, modifying headers, or even streaming HTML.

What is next used for?

js is an open-source web development framework created by Vercel enabling React-based web applications with server-side rendering and generating static websites.


1 Answers

It's a convention to prepend a return to exit the function. The alternative would be to use if-else if-else instead of just if. In this case you just want to exit the function and step further on your middleware chain.

You'll see this pattern quite often. For example, this is pretty common:

someFunction(function(err, result) {
    if (err) {
        return console.error(err);
    }

    console.log(result);
});

It's less nesting and reads easier to most poeple compared to this:

someFunction(function(err, result) {
    if (err) {
        console.error(err);
    } else {
        console.log(result);
    }
});

The first pattern also keeps you from accidentally calling next() twice or even more times in case you have some error in your if-else-logic. And that's exactly what's not supposed to happen with next() in that case you posted. It could call next() and still cause the redirect in any case.

like image 92
Num Lock Avatar answered Oct 13 '22 10:10

Num Lock