Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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);
    } else {
      //if url == "/" && POST

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

I get the following

Server running at http://<workspace-url>/                                                                                        
        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');