"Python Collections (2016, retired 2019)" was retired on January 6, 2020.

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

need some quick help about the get and post cycle going on here.

okay so what i conclude from all course till end is.. im gonna break it in short points so bear with me please. ill ask my questions in these points, will try to keep them simple. Im doing this so if theres fundamentally wrong i have understood will be corrected out.

1: We make a "POST" search field. 1.a : The data is being sent into the POST body to home route "/" which is the server we have. We do this because it's cleaner as andrew says and helps us with giving out info to outsiders via the url/query string.

2: We make moudles for routing,rendering of html data and give placeholders.
3:

//Handle HTTP route GET / and POST / i.e. Home
function home(request, response) {
  //if url == "/" && GET
  if(request.url === "/") {
    if (request.method =="GET"){
      //show search
      response.writeHead(200, commonHeaders);  
      renderer.view("header", {}, response);
      renderer.view("search", {}, response);
      renderer.view("footer", {}, response);
      response.end();

    }else {
        let body = "";
            request.on("data",(postBody)=>{
            let query = querystring.parse(postBody.toString());
            response.writeHead(303, {"Location" : "/" +query.username});
            response.end();

      });

    }
  }}

We say that if the method is get and homepage "/" simply show the homepage. now looking at this i conclude that The following code is a get request. Now im not entirely clear on how get requests look in url form. as andrew showing they look something like username?=chalkers so is

'http://port-3000-ffnm9mujbj.treehouse-app.com/chalkers' =username?=chalkers

or

 http://port-3000-ffnm9mujbj.treehouse-app.com/ = username?=chalkers

hence both are get requests?

4: Post request will never be shown in the URL as its in the body of the request. so all response or what is showing in the ur is a get method? 5: dont understand why we need to redirect post to get method.. 6: event emiiters are not explained. it was simply included in the profile.js file so im asumming its something we will overlook in express or something like that. i think that's it, the most i'm weak on is get and request and why we need to redirect it. Thank you.

2 Answers

Rebecca Jensen
Rebecca Jensen
11,580 Points

I think what you're asking about is how different URLs correspond to different HTTP requests. Have you covered "RESTful APIs" yet?

The difference is in both the URL and in the request TYPE (GET, POST, PUT, DELETE, etc). You can have the same URL but a different request TYPE to get a different result.

A plain "/" url (http://someurl.com/) and a GET type is generally used for "getting all" of a resource (e.g. all messages), whereas a url followed by an id (http://someurl.com/somemessageid) is used to get something specific, like a specific message.

A plain "/" and a POST type is used to create something (it doesn't have an id yet).

A "/someid" url is also used with EDIT and DELETE, as you need to know what it is you are editing or deleting.

Check out a RESTful API course, and this will make more sense!

Cheers

ty i havent covered that tho im going thru express and hopefully it will help me clear out stuff

Steven Parker
Steven Parker
231,275 Points

For a GET request with a query string, the query part starts with the question-mark. Also, there are no spaces between the URL and the query string:

http://port-3000-ffnm9mujbj.treehouse-app.com?username=chalkers

And I'd guess the redirection is just to avoid implementing the request handler two different ways.