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 Dealing with the POST Body

Carlos Lantigua
Carlos Lantigua
5,938 Points

querystring.parse() throws up console error, not sure why : (

when attempting to post the username data from the form field I get a weird error message

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

      //get the post data from body
        request.on("data", function(postBody) {
          //extract the username 
        let query = queryString.parse(postBody.toString());
          response.write(query.username);
          response.end();
          //redirect to /:username
      });
    }
  }
}

I get the following

Server running at http://<workspace-url>/                                                                                        
/home/treehouse/workspace/router.js:22 
        let query = queryString.parse(postBody.toString());                                                                      
                                ^                                                                                                

ReferenceError: queryString is not defined                                                                                       
    at IncomingMessage.<anonymous> (/home/treehouse/workspace/router.js:22:33)                                                   
    at emitOne (events.js:96:13)                                                                                                 
    at IncomingMessage.emit (events.js:188:7)                                                                                    
    at IncomingMessage.Readable.read (_stream_readable.js:381:10)                                                                
    at flow (_stream_readable.js:761:34)                                                                                         
    at resume_ (_stream_readable.js:743:3)                                                                                       
    at _combinedTickCallback (internal/process/next_tick.js:80:11)                                                               
    at process._tickCallback (internal/process/next_tick.js:104:9)

1 Answer

Carlos Lantigua
Carlos Lantigua
5,938 Points

figured it out, per the documentation is requires querystring to be defined as a variable, just dropped this at the top..

const querystring = require('querystring');