Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Jeremy Bowden
seal-mask
.a{fill-rule:evenodd;}techdegree
Jeremy Bowden
Full Stack JavaScript Techdegree Student 10,002 Points

Cookie stored in server as well as client?

Hi, In the Flashcards project (techdegree unit 6), when a POST request is received a cookie is "created" by Express and sent back to the client using the res.cookie() method:

router.post('/hello', (req, res) => {
  res.cookie('username', req.body.username);
  res.redirect('/');
});

And also immediately redirects to the / route, which is:

router.get('/', (req, res) => {
    const name = req.cookies.username;
    if (name) {
      res.render('index', { name });
    } else {
      res.redirect('/hello');
    }
});

So my first question is, does the /hello route a) redirect the CLIENT to the / route, or b) does it redirect itself straight to the / route, which then causes the 'index' template to be rendered (assuming if (name) === true)?

Second question is: if a) happens above then I guess the / route has received a new GET request from the client so the cookie is available, but if b) above is true, how would the / route have access to the coookie? (Unless is it also 'stored' on the server?)

Thanks you !!!