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 trialMaurice Tafolla- Cunningham
7,708 PointsError: 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' });
response.write("Header\n");
response.write("Search\n");
response.end("Footer\n")
}
//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' });
response.write("Header\n");
//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");
response.end("Footer\n");
});
//on "error"
studentProfile.on("error", function(error){
// show error
response.write(error.message + "\n");
response.end("Footer\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
e_1_video_3\router.js:43:14)
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
.JS\stage_1_video_3\profile.js:38:36)
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
Ernesto Emmanuel Gutierrez Muñoz
13,520 PointsHi 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
jsdevtom
16,963 PointsNode 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() {
response.end("Footer\n");
});
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.