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

Getting a bad gateway response when attempting to run the app

Hey guys! I am attempting to run the app, but I am getting a bad gateway 502 error. Here's my code for router.js:

var Profile = require("./profile.js"); var renderer = require("./renderer.js"); 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() === "") { 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
  var query = querystring.parse(postBody.toString());
  //redirect to /:username
  response.writeHead(303, {"Location": "/" + query.username});
  response.end();
  console.log(postBody.toString());
});

}

}

//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,
    javascript: profileJSON.points.JavaScript
  }
  //Simple response
  renderer.view("profile", values, response); 
  renderer.view("footer", {}, response);
  response.end();
});
//on "error"
studentProfile.on("error", function(error){
  //show errror
  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

Alonso Quesada
Alonso Quesada
14,782 Points

In your home function, in the second if statement:

if(request.method.toLowerCase() === "")

You are missing the "get" keyboard, and the browser can't evaluate it, it should be

if(request.method.toLowerCase() === "get")

Hope it helps.

Alonso Quesada this helped me immensely. Thank you!