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 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.