Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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 !!!