I’m new to node.js, and I want to know a few basic things:
-
Where to put the project files? Like for PHP we put them in
www/ORhtdocs/. -
I put them in a folder in my documents and tried
node /path/to/folder/example.js. Is this correct? -
I tried the very first program from node’s official doc, it just prints out
...to the console instead of printingServer is running at 127.0.0.1:8080. Could the folder structure be a reason for this? -
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!
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; sinceconsole.log()doesn’t actually return anything (it’s used only for its side effects), that’s why you get “undefined”.