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

tal Shnitzer
PLUS
tal Shnitzer
Courses Plus Student 5,242 Points

where can I find a code sample to create server? the video is outdated

the code that is bring copied in this video from node.js site is no longer there. where can I find a node code to create server.

3 Answers

Chris Pulver
Chris Pulver
7,624 Points

This is what I just used and was able to get it to work:

const http = require('http');

const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World!\n');
}).listen(port);
console.log(`Server running at http://:${port}/`);

After putting this in app.js, save, go to the console and type "node app.js" and then go to the preview button and select port 3000. Should show "Hellow World" in the new window.

I noticed the video was outdated as well. I found the code used in the video, however using the more modern const variables at: https://nodejs.org/en/docs/guides/getting-started-guide/ .

Dom Ss
Dom Ss
4,339 Points

You can always when you see that video is outdated, and this one is, its from 2015, search for official node documentation. https://nodejs.org/ is the source.

As a programmer, you have to learn how to search for information online. It's something I learned REALLY fast on the job. You google all the time in the beginning.

Hmm even with that, when you remove the server as a parameter from 'console.listen' that doesn't seem to work the same as it did in the example on the video