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

I solved the challenge with HTTP Method 'DELETE' and app.get. Any problems doing it this way?

I solved the challenge a little differently than Andrew in the video. There are two main differences:

  1. Instead of using the 'POST'-method I used the 'DELETE'-method. In the index.pug-file
extends layout.pug
     block content
          section#content
                   h2 Welcome, #{name}!
                        form(action='/goodbye', method='DELETE')
                             button(type='submit') Goodbye MoonMen
  1. Furthermore, instead of
app.post('/goodbye', (req, res) => {
   res.clearCookie('username');
   res.redirect('/')
}); 

I used

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

The alternative ways of solving it seem to work, but I just want to make sure that I have not missed anything by using this method instead. Any and all feedback are much appreciated!

1 Answer

Jamie Gobeille
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jamie Gobeille
Full Stack JavaScript Techdegree Graduate 19,572 Points

Hello!

I checked and this does seem to work alright in this use case. However, you would not be posting this information to your server, just handling the issue locally in the browser. I can't exactly say this for certain, but I believe your server would still have that cookie stored if you structured your routes this way because you post to the server the username cookie, but never make a post to go and delete it.

Also, you effectively have an extra route that's only purpose is to redirect back to hello. You can type http://localhost:3000/goodbye at any point and get a redirect back to hello. With the post route solution in the video, goodbye isn't an accessible route you can type into the browser to visit.

One other thing is that you don't need to pass in the res.body.username into the res.clearCookie method because the documentation only asks for the name of the cookie, not its value. You can see what I mean by opening the console and going to the application tab, there you will see username under the name and whatever you typed as the username as the value.

app.post("/goodbye", (req, res) => {

//"username" is the cookies name
//res.body.username is the value of the cookie

//This is all that is needed in this case
  res.clearCookie("username");
  res.redirect("hello");
});