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

JavaScript Build a Simple Dynamic Site with Node.js Creating a Simple Server in Node.js Creating a Simple Server

Error: write after end

This is actually my second time working through this course, because I keep getting errors. At this stage, I am encountering the following error:

events.js:85                                                                                                                                                     
      throw er; // Unhandled 'error' event                                                                                                                       
            ^                                                                                                                                                    
Error: write after end                                                                                                                                           
    at ServerResponse.OutgoingMessage.write (_http_outgoing.js:                                                                                                  
413:15)                                                                                                                                                          
    at Server.<anonymous> (/home/treehouse/workspace/app.js:13:                                                                                                  
12)                                                                                                                                                              
    at Server.emit (events.js:110:17)                                                                                                                            
    at HTTPParser.parserOnIncoming [as onIncoming] (_http_serve                                                                                                  
r.js:491:12)                                                                                                                                                     
    at HTTPParser.parserOnHeadersComplete (_http_common.js:111:                                                                                                  
23)                                                                                                                                                              
    at Socket.socketOnData (_http_server.js:343:22)                                                                                                              
    at Socket.emit (events.js:107:17)                                                                                                                            
    at readableAddChunk (_stream_readable.js:163:16)                                                                                                             
    at Socket.Readable.push (_stream_readable.js:126:10)                                                                                                         
    at TCP.onread (net.js:538:20)

I was able to preview the page, and it looked like the page in the video. The difference is, in the video, the console does not throw an error message. Now, a "write after end" error makes given that there is a "response.write" on the line after "response.end". However, since this is similar to the error I have encountered towards the end of the course, I thought I would ask about it.

Here's my code for the server:

var http = require('http');

http.createServer(function (request, response) {
  response.writeHead(200, {'Content-Type': 'text/plain'});
  response.write('This is before the end\n');
  response.end('Hello World\n');
  response.write('This is after the end\n');  
}).listen(3000);


console.log('Server running at http://<workspace-url>/');

1 Answer

I think I have figured out the cause of this. (not the solution). In Chrome (not tested in other browsers), if I type "chalkers" Chrome will send the GET request before I actually hit enter (if I have previously typed "chalkers", as if it is anticipating the url. (I see this because I added a massive amount of logging.) Once I hit enter, another "/chalkers" GET request is sent. So in my console logging I see two GET requests come in. The first GET request processes and sends the response and everything is displayed on the screen (which is why it always renders in the browser correctly). The second GET response sends the same data but then crashes the console app immediately after ending the response.

I can't be sure, but I only see it crash when I see Chrome sending to simultaneous GET requests.

UPDATE

In order to get Chrome to send two GET requests, you need to come from a different page, say "/", let that page load, then start added "chalkers" to the url in the address bar in Chrome, (it will send the request), and then hit Enter (then it will send the second request). Every time I do this, app.js crashes.

Minor Update: You have to do this a few times before Chrome adds in the additional 'smart' behavior of doing the request before you actually hit enter.

UPDATE (FIXED)

I fixed the issue if I bypass the code from Profile.js. Meaning, I take out the JSON request to treehouse and put it directly in router.js after the code that checks that the username.length > 0. So rather than doing:

var studentProfile = new Profile(username); studentProfile.on("end", function(profileJSON){

etc. etc.

I copy the profile request in from Profile.js (changing the request/response variable names since those are already defined) and execute it right there.

I still get multiple requests from chrome (which I don't understand why, because in a simple server example, I only get one, doing the exact steps described above), but for some reason the second response doesn't seem to kill the app any longer.

This leads me to believe the problem is in the .emit() or how we are using .emit(), although I admit I still don't understand why that would be, why we get two requests, or why the second request was crashing.