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

Darrell Osborne
Darrell Osborne
23,145 Points

Form 'method' for res.clearCookie(), does it matter?

In the clearing cookie challenge with the Express Basics course, the instructor sets the method to 'post' for the form (that contains the button) that triggers the clearCookie method. I did that same exact thing, but also checked it with a 'get' method and got the same results. I'm wondering, since we aren't sending any info to the server, does it even matter which method (get or post) we use? Is one better than the other in this instance?

2 Answers

Sam Donald
Sam Donald
36,305 Points

I believe at this time, given our code it does not matter. Because they both work as desired. I think it's more of a convention.

  • GET requests send the information in the url. And so the information is exposed.
  • POST requests are sent privately in the page headers. So in a real environment where you may be collecting sensitive user data on log-out you'd wont to use POST.
Darrell Osborne
Darrell Osborne
23,145 Points

I was thinking about that - the information being exposed within a GET method, and that POST would be better for more sensitive data. Thanks!

Tom Geraghty
Tom Geraghty
24,174 Points

I was wondering a similar thing; whether you can use an html link instead with an href that points to a route defined with the same functionality.

Is there a reason this would not work or would not be recommended in production?

index.pug
extends layout.pug

block content
  section#content
    if username
      h4 Hello, #{ username }!
      form(action='/logout', method='post')
        button(type='submit') Logout
server.js
app.post('/logout', (req, res) => {
  res.clearCookie('username')
  res.redirect('/')
})