V
V
vetsmen2017-10-20 20:49:10
JavaScript
vetsmen, 2017-10-20 20:49:10

How is return res.json() different from res.json()?

How is return res.json() different from res.json()?
And how is return next() different from next()?
In both cases, everything works the same way.

app.post('/login', function(req, res, next) {
  passport.authenticate('local', function(err, user, info) {
    if (err) return next(err);
    if (!user) {
      return res.status(401).json({ status: 'error', code: 'unauthorized' });
    } else {
      return res.json({ token: jwt.sign({id: user.id}, secret) });
    }
  })(req, res, next);
});

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Abcdefgk, 2017-10-20
@vetsmen

return is just the end of the function.
and

res.json({ token: jwt.sign({id: user.id}, secret) });
return;

This is the same.

I
Ivanq, 2017-10-20
@Ivanq

Well, let's see where we return. In passport.authenticate, right? And then what? - callback. And the result of the callback is processed? - No. Well, here's the answer - you return to the void.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question