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 Build a Simple Dynamic Site with Node.js HTTP Methods and Headers Redirection Headers in Node.js

Marcio Mello
Marcio Mello
7,861 Points

Why use POST if you're redirecting to a GET page?

I know there might be a good reason for that but, why is Andrew using POST method to redirect it to a GET page?

Wouldn't it be simpler to just use GET in the first place?

3 Answers

Well, I think you choose the right HTTP verb for your form but someone could do a HTTP request from cURL or some other tool and you could be redirect them to the right route. It's all about manage well all the requests you receive. And maybe for security reason it's better serves a normal page than an error page that can expose some information about your architecture.

Farid Wilhelm Zimmermann
Farid Wilhelm Zimmermann
16,753 Points

He did use a "POST", cause it is best practice to hide sensible data in the request body, instead of adding it to a query string. Although this might be not necessary, when performing a simple search for a username, it sure would if we'd include passwords, credit card information etc. in our project.

It also keeps our url cleaner and neater (which also provided a way to teach us about redirecting headers).

The second GET request was actually triggered by adding 303 to our HTTP Header Field.

response.writeHead(303, {"Location": "/" + username});

This second GET request was needed to kind of "refresh" our webpage, so that the profile.html would be shown, with our dynamically generated data included.

Sometimes on Treehouse, things might be solved differently, or might even seem to be unnecessarily complicated, but in the end, projects here are often designed this way to teach us various techniques that we will need for further projects.

He used get AND, if the user do a post, he redirect it to a get.

Marcio Mello
Marcio Mello
7,861 Points

Indeed he did. But wouldn't be faster and simpler to just use GET on the form?

The only "advantage" I see is not having the "?" of the query string on the final URL.