Newbie Node JS queries

I'm new to node.js, and I want to know a few basic things:

  1. Where to put the project files? Like for PHP we put them in www/ OR htdocs/.

  2. I put them in a folder in my documents and tried node /path/to/folder/example.js. Is this correct?

  3. I tried the very first program from node's official doc, it just prints out ... to the console instead of printing Server is running at 127.0.0.1:8080. Could the folder structure be a reason for this?

  4. Also is it possible to make a whole website using node.js OR only specific modules can be made? What kind of modules?

Please guide me!

Thanks!

Edit:

This is the code I tried, its the very first 'Simple Web Server' example from Node's docs:

var http = require('http');

http.createServer(function (request, response) {
    response.writeHead(200, {'Content-Type': 'text/plain'});
    response.end('Hello World\n');
}).listen(8124);

console.log('Server running at http://127.0.0.1:8124/');

Edit:

This code just prints out the foll:

>node example.js
...

Doesn't even get back to the node prompt!

  1. It doesn't really matter where you place the files as long as the user that runs the node command has access to that location.

    In fact, I would recommend that you place node.js source files outside any www or htdocs folder (or any publicly avaiable folder for that matter), because else the .js source file could be world readable when served using your webserver (including any security credentials that might be in the source files, leaving you exposed).

  2. Yes, that is a correct way of running a node script. I would however recommend to first cd into the correct directory and running node example.js from there to prevent path resolution errors, especially when you start using various npm modules later on.

  3. Probably not. It's hard to tell without seeing the code you're trying to run first, but if any path resolution errors would occur due to folder structures etc, node would just exit with a nasty error instead of printing '...'.

  4. Yes, it's possible to build an entire website using node. In fact, there are various npm modules that can help you making that easier. I recommend you take a look at:

    These frameworks provide lots of bells and whistles that extend node to make it more feasible as a full fledged webserver (cookies, vhosts, sessions, path routing, etc.)

EDIT

To elaborate on answer nr.3: I've tried the code you pasted, both from the correct folder as I've recommended in answer nr.2 and using an absolute path; They both work fine:

remco@Prosperpine ~ $ node tests.js 
Server running at http://127.0.0.1:8124/
^C

remco@Prosperpine ~ $ cd /
remco@Prosperpine / $ node /Users/remco/tests.js 
Server running at http://127.0.0.1:8124/

So the node server starts up and is serving content on http://127.0.0.1:8124/, which works as well:

remco@Prosperpine ~ $ wget http://127.0.0.1:8124/ && cat index.html
--2012-07-03 13:25:32--  http://127.0.0.1:8124/
Connecting to 127.0.0.1:8124... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/plain]
Saving to: `index.html'

    [ <=>     ] 12          --.-K/s   in 0s      

2012-07-03 13:25:32 (901 KB/s) - `index.html' saved [12]

Hello World
remco@Prosperpine ~ $ 

So we can conclude that there's nothing wrong with the code you are trying. Probably there's something wrong with your node installation. Is this on Linux, OSX or Windows?

Your problem is that you're trying to run your server from inside node's REPL. You need to run it from the Windows command line directly. The "..." you're getting means that node's REPL hasn't seen the end of a valid JavaScript expression and so is waiting for you to enter more.

In response to your last comment on the previous answer, the reason the REPL is printing "undefined" after calling console.log() is that it's evaluating it as an expression (which happens to have a side effect, namely printing out the message) and then trying to print its return value; since console.log() doesn't actually return anything (it's used only for its side effects), that's why you get "undefined".