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

502 Bad Gateway, starting with the changes with request around the 7:00 mark

Here's my router.js file

var Profile = require("./profile.js");
var renderer = require("./renderer.js");

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() === ""){
      //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) {
        var body = "" + postBody;
        console.log(postBody.toString());
      });
      // extract the username
      // redirect to /:username
    }
  }  

}

// Handle HTTP route GET /:username i.e. /chalkers or /robheath
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;

3 Answers

Rewatched the video with a fine-toothed comb and found I had missed. On line 10, I had left off "get"

EDIT: Nope! Still get the 502 error after the additions at the 7:00 mark.

i was having the same issue i re-wrote my code and noticed my closing "{}" braces were over-lapping with the function and if statement.

 //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 {
        request.on("data",(postBody)=>{
        console.log(postBody.toString());
      });

    }
  }}

i know it's a old post but sometimes stuff just keeps bugging you lol. hope this helps you and puts you at ease.

Pradeep Pai
Pradeep Pai
6,647 Points

I received 502 bad request as well. Changed "function(postBody)" to "(postBody) =>". Also, request.method == "GET" and it worked.