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 trial![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Sorin Chircu](https://secure.gravatar.com/avatar/9f1110ec5157332788b1a9819da4159d?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Sorin Chircu
45,432 PointsERR_STREAM_WRITE_AFTER_END when hosting locally.
So, I got to the end of this video, and when I run the server it works, when I open the page it works, when I put the profile to an invalid one it works, but if it's a valid profile, I get the following error:
Server running at http:127.0.0.1:1337
events.js:167
throw er; // Unhandled 'error' event
^
Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at write_ (_http_outgoing.js:572:17)
at ServerResponse.write (_http_outgoing.js:567:10)
at Profile.<anonymous> (D:\Documents\scripts\Build a Simple Dynamic Site with Node.JS\stage_1_video_3\router.js:38:22)
at Profile.emit (events.js:182:13)
at IncomingMessage.<anonymous> (D:\Documents\scripts\Build a Simple Dynamic Site with Node.JS\stage_1_video_3\profile.js:38:36)
at IncomingMessage.emit (events.js:187:15)
at endReadableNT (_stream_readable.js:1094:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
Emitted 'error' event at:
at writeAfterEndNT (_http_outgoing.js:634:7)
at process._tickCallback (internal/process/next_tick.js:63:19)
The code of my router.js is the same as in the video, I even tried copying and pasting the entire "finished" version of the code into my code to make sure I had zero typing mistakes. The app.js is also the same, excepting the port number I'm using and the text that gets output by the console.log command.
var router = require("./router.js");
// Problem: Simple way to look at user's badget count and JS points from browser
// Solution: Use node.js to performe profile lookups and server our template via HTTP
//1. Create a web server
var http = require('http');
http.createServer(function (request, response){
router.home(request, response);
router.user(request, response);
}).listen(1337);
console.log('Server running at http:127.0.0.1:1337');
//2. Function that handles reading of files and merge in values
//Read from file and get string
//merge values into string
var Profile = require("./profile.js");
//1. 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("Header\n");
response.write("Search\n");
response.end("Footer\n");
}
//response.end('Hello world!\n');
//if url === "/" && POST
//redirect to username
}
//2. Handle HTTP route GET /:username i.e /chalkers
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 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("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;
Honestly, I have no clue what could be causing this. Any guesses?
3 Answers
![Adam Beer](https://uploads.teamtreehouse.com/production/profile-photos/8766742/micro_DSC_0833_m_solat__1_.jpg)
Adam Beer
11,314 PointsInteresting issue. Because I tried your code and it works fine to me. I saw you use the server, but maybe this is the bug. Stop the server, then restart. What happend?
![Matt Davis](https://uploads.teamtreehouse.com/production/profile-photos/10287822/micro_IMG_0166.jpg)
Matt Davis
13,372 PointsI'm getting the same error did you ever figure this out?
![JASON LEE](https://secure.gravatar.com/avatar/798eb96d4c2cc69af1ecaf0ccb0b8be5?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
JASON LEE
17,352 PointsIt works fine on my end. All I did was change the server to the local host. But it shouldn't have changed anything relating to the error message.
var http = require('http');
http.createServer(function (request, response){
router.home(request, response);
router.user(request, response);
}).listen(3000, '127.0.0.1', "");
console.log('Server running at http://127.0.0.1:3000');
Sorin Chircu
45,432 PointsSorin Chircu
45,432 PointsI tried that, yeah, even uninstalled Node.js v10 and installed v8 under recommendation from someone and it still crashes. I am so confused.