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 Node.js Basics (2014) Introduction to Node.js Hello World

Gary Calhoun
Gary Calhoun
10,317 Points

I am trying to understand something about node.js

I was able to get node.js running successfully on my windows pc, and it also installed npm. What I am confused about it is when you click the node js application on windows it opens up a blank command prompt, so which commands work in here because node -v for example doesn't work. I can however do it in my windows command prompt with no problem. How does node.js work as a server do you need a dedicated machine to run it when you have a live site or do most webhosts allow for node.js by default?

Tyler _
Tyler _
6,651 Points

most web hosts should allow it.

1 Answer

Node.js is a runtime environment. Launching the node cli opens the Node REPL (Read-Eval-Print Loop), which is basically a Node console. You can execute javascript in this REPL.

The command node -v will print the node version from the normal command line if node is installed. The reason why it did not work in your case is because you were already inside the REPL, which only knows how to evaluate javascript.

To use node to start a server, you would need to require the http module and use it:

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

As for hosting node.js applications, look here

Hope this helps!

Gary Calhoun
Gary Calhoun
10,317 Points

Thanks for the code example and link! This clears it up I didn't realize the node windows application goes into the Read-Eval-Print Loop by default.