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

With ES6 and Nodes new documentation

With the new ES6 and Nodes updated documentation (it's written using the new stuff from ES6, example:

(`Server running at http://${hostname}:${port}/`);

), I feel like it is appropriate that Workspace gets updated so you can use it in there too, if it haven't already - I think I ran over a problem where I tried writing code like that, but it didn't work. Though I didn't test anything out, so maybe it does work, and I am just a dumb dumb.

What happened was that the console threw an error, when I tried running a script with code written like the example above.

2 Answers

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Hi Anna Grais

We're looking to upgrade to the latest long term support of Node.js for students!

Keep an eye out!

Regards,
Andrew

To help with anyone with this question on the node.js website under the "Documentation" heading just look for "Web Server" for an example.

As of today this is the code provided for Node.js 8.0.0 which is written in ES6. You should be able to change the relevant parts of the code using the video and skills learned up to this point on the Javascript track.

Current link -> web server

//will respond "Hello World" to the console if initialized correctly.

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

To run the server, put the code into a file called "yourfilename.js" and execute it with Node.js:

//change to the correct file name by replacing "yourfilename.js"
$ node yourfilename.js
Server running at http://127.0.0.1:3000/