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 trialJason Berteotti
12,352 Pointsevents.js:85 is throwing an unhandled 'error' event after displaying a profile's info on the page.
after i place /chalkers or any profile url, the JSON is received and the information is displayed correctly, but the node stops running and displays this error:
events.js:85
throw er; // Unhandled 'error' event
^
Error: write after end
at ServerResponse.OutgoingMessage.write (_http_outgoing.js:413:15)
at Profile.<anonymous> (/home/treehouse/workspace/router.js:40:16)
at Profile.emit (events.js:107:17)
at IncomingMessage.<anonymous> (/home/treehouse/workspace/profile.js:38:36)
at IncomingMessage.emit (events.js:129:20)
at _stream_readable.js:908:16
at process._tickCallback (node.js:355:11)
Andrew VanVlack
20,155 PointsInterestingly I was unable to reproduce the error at all. Made about 30 calls to the node server and none gave the error.
Shane Kercheval
2,036 PointsI'm getting the same error, on occasion.
4 Answers
Andrew VanVlack
20,155 PointsThe link to your work space seems to be dead so if you can fix that I can take a look or just paste in your code for your router.
You normally get that error when you try to write a response back after you respond with end.
Check around line 40, your last line in your http request 'on end' function should be something like:
response.end()
Engine Digital
7,925 PointsI think the thing is to actually move the response.end
from the bottom of the user function, not just copy it. This is what throws the error.
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 PointsI had the same problem, and I'm glad this thread exists!! Just to add one more thing - you got this error by doing something that you were told to do. We were instructed to put that write statement in there as part of proving a point about how things after response.end won't get written. If we had been looking at his console, we probably would have seen the same error, and it would have been nice to have a heads up about this error message.
Darryn Smith
32,043 PointsI am still getting this error (events.js:85) after changing all of my response.end statements. They no longer include anything in quotes. The "Jim has 2 badges" message displays and the server quits with the error mentioned in the OP.
I have no response.write() statements after the response.end() statement.
What's up with this?
First, here is the error log:
treehouse:~/workspace$ node app.js
Server running at http://<workspace-url>/
events.js:85
throw er; // Unhandled 'error' event
^
Error: write after end
at ServerResponse.OutgoingMessage.write (_http_outgoing.js:413:15)
at Profile.<anonymous> (/home/treehouse/workspace/router.js:39:13)
at Profile.emit (events.js:107:17)
at IncomingMessage.<anonymous> (/home/treehouse/workspace/profile.js:38:36)
at IncomingMessage.emit (events.js:129:20)
at _stream_readable.js:908:16
at process._tickCallback (node.js:355:11)
treehouse:~/workspace$
And here is my current router.js code:
var Profile = require("./profile.js");
// Handle HTTP route GET / and POST / i.e. Home
function home(request,response) {
//if url == "/" && GET
if(request.url === "/") {
//show search
response.writeHead(200, {'Content-Type': 'text/plain'});
response.write("Header1\n");
response.write("Search1\n");
response.end();
}
//if url == "/" && POST
//redirect to /:username
}
// Handle HTTP route GET /:username i.e. /darrynsmith
function user(request, response) {
//if url == "/...."
var username = request.url.replace("/", "");
if(username.length > 0) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.write("Header2\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
}
//Simple response
response.write(values.username + " has " + values.badges + " badges\n");
response.end();
});
//on "error"
studentProfile.on("error", function(error){
//show error
response.write(error.message + "\n");
response.end();
});
}
}
module.exports.home = home;
module.exports.user = user;
PS: the https 'require' and code fixes have been properly implemented in profile.js:
var EventEmitter = require("events").EventEmitter;
var http = require("http");
var https = require("https");
var util = require("util");
//******************* skip *******************
var request = https.get("https://teamtreehouse.com/" + username + ".json", function(response) {
var body = "";
if (response.statusCode !== 200) {
request.abort();
//Status Code Error
profileEmitter.emit("error", new Error("There was an error getting the profile for " + username + ". (" + http.STATUS_CODES[response.statusCode] + ")"));
}
gabriel ferreira
3,110 Pointsif anyone has the same problem:
make sure that you dont had any "response.write" before the "response.writeHead"
Jason Berteotti
12,352 PointsJason Berteotti
12,352 Pointshttps://w.trhou.se/2wtm9olgng
So, after trying it out several more times, I have found that sometimes it runs fine and displays the badges and continues running, and other times throws the above error. This occurs only on valid usernames; errors work perfectly.