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 Creating a Basic Template Engine in Node.js Reading from Files

Jonathan Leon
Jonathan Leon
18,813 Points

Error: write after end I get this error emitted after trying to add response.end() like in the video

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


// handles the http  route GET / POST i e home


function home(request,response) {





    //if the url = "/ && GET " 

  if (request.url === "/") {
        //show the search field
    response.writeHead(200, {'Content-Type': 'text/plain'});    
    renderer.view("header" , {} , response);
    renderer.view("search" , {} , response);
    renderer.view("footer" , {} , response);
    response.end();

  }
    //if the url = "// && POST " 
        //redirect to the username
}


//handles the http route get / username i.e /jonathanleo

function user(request,response) {

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

//if the url = "//..."

  if (username.length > 0 ) {


    response.writeHead(200, {'Content-Type': 'text/plain'});    
    renderer.view("header" , {} , response);
    response.end();

//get the JSOn from treehouse  

      var studentProfile = new Profile(username);


//on end
        studentProfile.on("end", function(profileJSON){

//show the 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 the 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;
Jonathan Leon
Jonathan Leon
18,813 Points

That's my renderer file

var fs = require("fs");


function view(templatename , values , response) {

 //read from the template files

var filecontents =  fs.readFileSync('./views/' +templatename+ ".html" );

  //insert values into the content


  //write out the contents to the response
  response.write(filecontents);





  //write out to the response





};


module.exports.view = view;
Jonathan Leon
Jonathan Leon
18,813 Points

Okay I figured it out, I had "response.end()" after these lines of code and that was the problem

  if (username.length > 0 ) {


    response.writeHead(200, {'Content-Type': 'text/plain'});    
    renderer.view("header" , {} , response);    

can someone explain me why this function ran and username.length > 0 was true when my request was just the home page? isn't "".length = 0?

Glad you figured out the response.end(); issue.

about your secondary question about why .length > 0 evaluates to true for the home page.

Mine is doing the same thing. Were you able to figure it out? Were you running yours locally as I am?