K
K
komigor2021-02-01 21:45:58
Node.js
komigor, 2021-02-01 21:45:58

What does such a code mean?

I can't figure out what it means res.redirect? and even more incomprehensible what he does in conjunction with return?

exports.getLogin = (req, res) => {
if (req.user) {
    return res.redirect('/'); // 
  }
  res.render('account/login', {
    title: 'Login'
  });

Then this construction is used in the app.js file like this:
app.get('/login', userController.getLogin);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vasily Bannikov, 2021-02-01
@komigor

Have you tried reading the documentation?
expressjs.com/en/api.html#res.redirect
And in combination with return it does the same thing as

res.redirect('/');
return;

Together with the condition and res.render at the end, this gives:
If the user is logged in, then we redirect him to the page /, otherwise we give him the login page.

A
Alexander Pinkevich, 2021-02-01
@pinkevich

return is needed here to exit the getLogin function before rendering the login page, provided that the user is logged in.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question