JavaScript Build a Simple Dynamic Site with Node.js Handling Routes in Node.js Populating User Information

Maurice Tafolla- Cunningham
Maurice Tafolla- Cunningham
7,708 Points

Error: write after end

I cannot keep my server online, without errors for more than a few username searches. After a short amount of time in the console I am given this error: Error: write after end.

This error is making a reference to my code in line 43 of my router.js file I'll mark it in my code below. A few things to note, I am running my own node environment on my local machine using port 1337:

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

// Handle the HTTP route GET / and POST  / i.e. Home
    function home(request, response) {
        //if url = "/" && GET
            //url is a property of the incoming message
        if (request.url === "/") {
                //show search field
            response.writeHead(200, { 'Content-Type': 'text/plain' });

            //if url = "/" && POST
                //redirect to /:username

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

    if (username.length > 0) {
        response.writeHead(200, { 'Content-Type': 'text/plain' });
            //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
                //****!!!!!!LINE 43 IS RIGHT BELOW THIS COMMENT!!!!!!!!****
                response.write(values.username + " has "+ values.badges + " badges\n");
            //on "error"
            studentProfile.on("error", function(error){
            // show error
                response.write(error.message + "\n");



    module.exports.home = home;
    module.exports.user = user;

Here is the error in its entirety in my console:

Error: write after end
    at ServerResponse.OutgoingMessage.write (_http_outgoing.js:430:15)
    at Profile.<anonymous> (C:\Users\Maurice\Desktop\Coding Reference\node\Build a Simple Dynamic Site with Node.JS\stag
    at emitOne (events.js:77:13)
    at Profile.emit (events.js:169:7)
    at IncomingMessage.<anonymous> (C:\Users\Maurice\Desktop\Coding Reference\node\Build a Simple Dynamic Site with Node
    at emitNone (events.js:72:20)
    at IncomingMessage.emit (events.js:166:7)
    at endReadableNT (_stream_readable.js:905:12)
    at nextTickCallbackWith2Args (node.js:442:9)
    at process._tickCallback (node.js:356:17)

2 Answers

16,963 Points

Node is asynchronous, which means that functions happen at the same time. Write after end means that something is being written after the program has ended.

Looking at this answer and combining it with this part of the docs for node.js, I think you should be doing something like this instead:

response.write(values.username + " has "+ values.badges + " badges\n", function() {

Now, I am tired, and I have been editing this code directly in the answer form and I haven't watched this video before answering so cutting of the slack would be appreciated.

Ernesto Emmanuel Gutierrez Muñoz
Ernesto Emmanuel Gutierrez Muñoz
13,520 Points

Hi Maurice After a couple minutes from trying to fix the same problem that you had i figure out that is not an error, the problem is when you search an student that doesn't set a photo image from his profile it throws this message But if you look an student who has some photo in his profile everything would be all right like our instructor chalkers search at forums more students with photo in his profile and you will see it I hope it may help Greetings