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 Express Basics Deeper into Routing with Express Solution: Clearing Cookies and Redirecting

Sean Flanagan
Sean Flanagan
33,235 Points

What's wrong with this?

Hi. My solution to this was to create a new file, goodbye.pug.

extends layout

block content
  form(action="/goodbye", method="post")
    label Goodbye!

In app.js, the goodbye code:

app.get("/goodbye", (req, res) => {
  res.cookie("username", req.body.username);
  res.redirect("/hello");
});

When I tested this, nothing was different from before.

5 Answers

Avi Barzilay
Avi Barzilay
12,326 Points

Hello! Its ok, because in this way you trying to GET route '/goodbuy', and GET method is not exist for '/goodbuy'. But you have POST method, and it works when you click on button. For checking if it works go to homepage, enter your name in form-field and press submit, you have to see welcome content with your name. After this click button 'Good Bye' and app will redirect you to '/hello' page with empty form for filling it.

Avi Barzilay
Avi Barzilay
12,326 Points

Hey! Almost right, but i missed one more thing. Replace in app.js get method to post. And you dont have to clear all cookies in dev tools manually, button goodbye should delete username from cookie. When you entered your name and submit it - appeared Welcome message with your name. This name exists in cookie-file. When you pressed goodbye button - username deletes from cookie and redirect you to '/hello' page.

Avi Barzilay
Avi Barzilay
12,326 Points

Hello, Sean!

  1. In goodbye.pug you have to add button for action, not just label. Replace 'label' to 'button'.
  2. In app.js you have to clear cookie before try to redirect to '/hello'. In your code you sending it. Replace res.cookie("username", req.body.username) to res.clearCookie("username") and it will work
Sean Flanagan
Sean Flanagan
33,235 Points

Hi Avi! Thanks for your help!

I've changed goodbye.pug. Is this what it should be?

extends layout

block content
  form(action="/goodbye", method="post")
    button Goodbye!

And app.js:

app.get("/goodbye", (req, res) => {
  res.clearCookie("username");
  res.redirect("/hello");
});

I ran localhost:3000/goodbye, entered my name, clicked the Submit button and a cookie appeared in Dev Tools. The welcome message appeared and I clicked Clear All in Dev Tools to delete the cookie.

Is that right?

Sean

Sean Flanagan
Sean Flanagan
33,235 Points

Hi Ari. Sorry for the delay.

I changed get to post. I ran node app.js. I opened localhost:3000/goodbye but the output was Cannot GET /goodbye. I checked the console but it contained a 404 (not found) error message.

Sean Flanagan
Sean Flanagan
33,235 Points

Hello Avi. It works now. Thank you!