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 Binding Values

Sergio Cruz
Sergio Cruz
15,550 Points

Error: write after end?

I keep getting this error message on the console when adding different usernames to the url. It happens randomly, sometimes after the second username, others after the third or at the second.

This is my code for the route.js

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

 //Handle HTTP route GET/ and Post/ i.e Home
 function home (request,response){
    //if url == "/" and GET
    if(request.url === "/"){
        //show search
        response.writeHead(200, {'Content-Type': 'text/plain'});
        renderer.view('head',{},response);
        renderer.view('search', {},response);
        renderer.view('footer', {},response);
        response.end();
    }
}

 //Handle HTTP route GET /:username i.e /chalkers
 function user (request,response){
    //if url === "...."
    var username = request.url.replace("/", "");

    if (username.length > 0){
        //get JSON from treehouse
        var studentProfile = new Profile(username);
        console.log(studentProfile)
        response.writeHead(200, {'Content-Type': 'text/plain'});
        renderer.view('head',{},response);
        //on 'end'
        studentProfile.on("end", function(profileJSON){
            //show profile
            //Store values
            var values = {
                avatarUrl : profileJSON.gravatar_url,
                username: profileJSON.profile_name,
                badges: profileJSON.badges.length,
                points: 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;

This is for the renderer.js

var fs = require("fs") //file system

function mergeValues (values,content){
//Insert values into the content
    for (var key in values){
        content = content.replace("{{"+key+"}}",values[key]);
    }   
    return content
}

function view (templateName,values,response){
    //Read from the template file
    var fileContents = fs.readFileSync("./views/"+templateName+".html", {encoding: "utf8"});
    fileContents = mergeValues(values,fileContents);
    //Write out to the response
    response.write(fileContents);
}

module.exports.view = view;

3 Answers

I had a similar problem. I fixed it by sending the encoding as a string instead of an object. so instead of {encoding: "utf8} i just put 'utf8' (double quotes didn't seem to work for some reason).

  function view (templateName, values, response) {
    var file = fs.readFileSync('./views/'  + templateName + '.html',  'utf8');
    file = mergeValues(values, file);
    response.write(file);
}

Hope that helps.

Tyler Taylor
Tyler Taylor
17,939 Points

This worked for me. This course is driving me crazy!

jsdevtom
jsdevtom
16,963 Points

I also have the same problem. I will reply here if I find an answer. If I don't, I have lost my mind

Rodrigo Valladares
Rodrigo Valladares
9,708 Points

Hey, i have the same problem. some ideas?