Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

James Barrett
James Barrett
13,253 Points

Node sever not fully functioning

Hi guys, I run node app.js and it only runs until I preview in port 3000. before it decides to throw an 'Unhandled error event' and crashes the app. Here is my code:

// Problem: A simple way to look at a user's badge count and JavaScript points from a web browser
// Solution: Use Node.js to perform the profile looks up

// 1. Create a web 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");
  setInterval(function() {
    response.write(new Date() + "\n"); 
  }, 1000);
  response.end('Hello World\n');
}).listen(3000);

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

// 2. Handle HTTP route GET / and POST / i.e. Home
  // if url == "/" && GET
    // show search
  // if url == "/" && POST
    // redirect to username

// 3. Handle HTTP route GET/ username i.e /jamesbarrett95

// 4. Function that handls the reading of files and merge to value

Thanks, James.

Ryan Zimmerman
Ryan Zimmerman
3,854 Points

.createServer() I believe wants to return a process so you need to assign it const server = http.createServer((req, res) => { res.end(); });

Seth Kroger
Seth Kroger
56,406 Points

It would help to know the rest of the error message.

Ryan Wittrup
Ryan Wittrup
9,156 Points

Hi James - the reason for this is because it's SUPPOSED to error out

It threw me off for a minute too because I try to pause the video and work ahead on my own and was wondering why it wasn't working. The timeout call (1000 ms) on the setInterval timer will not hit until the code has already run the res.end() line - so it will generate an error

2 Answers

James Barrett
James Barrett
13,253 Points

Ryan Zimmerman Okay, I have been exploring the documentation and trying to listen a bit more carefully in Andrew's video. I have also used ES6 syntax after doing a quick research on the arrow syntax (seems to make sense!)...

const http = require('http');
const port = 3000;

const server = http.createServer((req, res) => {
  res.write("hello world");
  setInterval(() => {
    res.write(new Date());
  }, 1000);
  res.end();
});

server.on('clientError', (err, socket) => {
  socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
});

server.listen(port, () => {
  console.log("Server started on port " + port)
});

I think the problem with my code was the res.end(). It seems clear that the server will not listen for anything after the .end method is called. :P

Thanks, James.

Ryan Zimmerman
Ryan Zimmerman
3,854 Points

const server = http.createServer((req, res) => { res.end(); });