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

Yongshuo Wang
Yongshuo Wang
5,500 Points

NodeJS redirect not working

I following this tutorial, but when I try to redirect to user method. It does not work, the page stays in gray color. Here is my snapshot of workspace https://w.trhou.se/ga329edluc

function home(request, response){
  if (request.url === '/'){
    if (request.method.toLowerCase() === "get"){
      response.writeHead(200, {'Content-type':'text/html'});
      render.view('header',{}, response);
      render.view('search',{}, response);
      render.view('footer',{}, response);
      response.end();
    }else{
      request.on("data", function(postBody){
        var query = queryString.parse(postBody.toString());
        response.writeHead(303, {'Location': '/' + query.username });

        response.end();
      });
    }
  }
}
simhub
simhub
26,544 Points

do you get an error message from the console?

i am getting the same result. were you able to figure it out?

simhub no error messages from the console.

Here is a snapshot of my workspace for anyone that wants to try my code. It still isn't working . https://w.trhou.se/z0dm27fnzl

Yongshuo Wang
Yongshuo Wang
5,500 Points

Cernan Bernardo There is a typo in your router.js user function, when you check the username's length, you need to use username.length rather than user.name.length.

3 Answers

simhub
simhub
26,544 Points

i looked at your snapshot.

actually there is a console error message:

events.js:85 throw er; // Unhandled 'error' event ^ Error: write after end at ServerResponse.OutgoingMessage.write (_http_outgoing.js:413:15) ................. at Object.user (/....../router.js:34:10)

..................

i am not sure what it means. i can just surmise that somewhere in your router.js file 'a response object is being written to after already being closed.'

so in the router.js file you could try to modify the user function like this:


function user(request, response){ console.log("Inside user function");

var username = request.url.replace("/","");

//---> DON'T PUT IT HERE !!! response.writeHead(200, {'Content-type':'text/html'}); render.view('header',{}, response); //--->

if (username.length > 0){

//---> PUT IT HERE !!! response.writeHead(200, {'Content-type':'text/html'}); render.view('header',{}, response); //---> .....

..........

but like i say'd before i do not fully understand what this error message is trying to say so if you figure that out please tell me :)

Yongshuo Wang
Yongshuo Wang
5,500 Points

From my console, I did not see any error, but following your suggestion, it works.

Thank you, your comment helped me too. I had the same error.

Thank you, your comment helped me too. I had the same error.

I have the same Issue people. Need help. I get an error "There was an error getting the profile for chalkers. (Moved Permanently)" when I access Localhost:3000/chalkers.

Here is the 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() === "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
    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;

I dont get it...