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

Nathan Boaldin
PLUS
Nathan Boaldin
Courses Plus Student 12,545 Points

Why will terminal not output postBody?

I am working locally. I do not understand why terminal will not output post body. The problem started when I just tried to log the buffer. I also get this error in chrome:

This page isn’t working localhost didn’t send any data. ERR_EMPTY_RESPONSE

My router code here:

var Profile = require("./profile.js");
var renderer = require('./renderer');
var querystring = require('querystring');
var commonHeaders = {'Content-Type': 'text/html'};

//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();
    }
    //if url == "/" && POST
  } else if (request.method.toLowerCase() === "post"){
    //get POST data from body
    request.on("data", function(postBody) {
      //extract username
      var query = querystring.parse(postBody.toString());
      response.write(query.username);
      response.end();
      //redirect to /:username
    });


  }
}

//Handle HTTP route GET /:username i.e. /chalkers
function user(request, response) {
  //if url == "/...."
  var username = request.url.replace("/", "");
  if(username.length > 0) {
    response.writeHead(200, commonHeaders);
    renderer.view("header", {}, response);

    //get json from Treehouse
    var studentProfile = new Profile(username);
    //on "end"
    studentProfile.on("end", function(profileJSON){
      //show profile

      //Store the values which we need
      var values = {
        avatarUrl: profileJSON.gravatar_url,
        username: profileJSON.profile_name,
        badges: profileJSON.badges.length,
        javascriptPoints: profileJSON.points.JavaScript
      }
      //Simple response
      renderer.view("profile", values, response);
      renderer.view("footer", {}, response);
      response.end();
    });

    //on "error"
    studentProfile.on("error", function(error){
      //show error
      renderer.view("error", {errorMessage: error.message}, response);
      renderer.view("search", {}, response);
      renderer.view("footer", {}, response);
      response.end();

    });

  }
}

module.exports.home = home;
module.exports.user = user;

1 Answer

Dimitrius Ionov
Dimitrius Ionov
17,737 Points

your if else statement is

if(request.url == "/") {

} else if (request.method.toLowerCase() === "post"){

});

check your '}', you have an extra one after you check for 'get' method