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

Gary Calhoun
Gary Calhoun
10,317 Points

Having a tough time working with this...

Workspaces doesn't work when using the ports I am doing it locally I am using nodemon now so I can just type nodemon app.js it runs the server but as soon as I go to view in browser I set it say Hello World and all that then I get the following

E:\xampp\htdocs\nodejs_practice\interactive_website>nodemon app.js 10 Aug 19:55:45 - [nodemon] v1.4.0 10 Aug 19:55:45 - [nodemon] to restart at any time, enter rs 10 Aug 19:55:45 - [nodemon] watching: . 10 Aug 19:55:45 - [nodemon] starting node app.js Server running at http://127.0.0.1:1337/ events.js:85 throw er; // Unhandled 'error' event ^ Error: write after end at ServerResponse.OutgoingMessage.write (_http_outgoing.js:413:15) at null.<anonymous> (E:\xampp\htdocs\nodejs_practice\interactive_website\app .js:12:12) at wrapper [as _onTimeout] (timers.js:274:14) at Timer.listOnTimeout (timers.js:119:15) 10 Aug 19:56:02 - [nodemon] app crashed - waiting for file changes before starting...

my code

var http = require('http');
http.createServer(function (request, response) {
  response.writeHead(200, {'Content-Type': 'text/plain'});
  //do soemthing every so many milliseconds
  setInterval(function(){
  response.write(new Date() + "\n");
//print every 1 second
  }, 1000);
  response.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

1 Answer

Stephen Layton
Stephen Layton
8,643 Points

Your calling response.end() which is ending the response before the write completes. Since your write is inside setInterval it will occur every second however the event loop continues on so the code will continue executing and run the response.end() multiple times. You can see this in action if you put a couple console.log()'s in the program. One as the callback to reponse.end and one in the setInterval should do the trick. Run with node index.js (nodemon doesn't give too much detail by default).

var http = require('http');
http.createServer(function (request, response) {
  response.writeHead(200, {'Content-Type': 'text/plain'});
  //do soemthing every so many milliseconds
  setInterval(function(){
  response.write(new Date() + "\n");
  console.log('write');
//print every 1 second
  }, 1000);
  response.end('Hello World\n', function () {
    console.log('end');
  });
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');


$ node index.js
Server running at http://127.0.0.1:1337/
end
end
end
write
events.js:141
      throw er; // Unhandled 'error' event
      ^

Error: write after end
    at ServerResponse.OutgoingMessage.write (_http_outgoing.js:413:15)
    at null._repeat (E:\node\treehousenode1\index.js:6:12)
    at wrapper [as _onTimeout] (timers.js:270:19)
    at Timer.listOnTimeout (timers.js:89:15)
Gary Calhoun
Gary Calhoun
10,317 Points

Thanks I understand what you mean now. I didn't know nodemon doesn't give alot of info either. I was using it because when you make a change it restarts the server and was having issues with that. I understand exactly whats going on now.