a blog for those who code

Monday 23 June 2014

Run NodeJs examples from CMD instead of Node.js

Have you just started learning Node.js and figuring out how to run your first simple application.
NodeJs Documentation says that -



An example of a web server written with Node which responds with 'Hello World':

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/');

To run the server, put the code into a file called example.js and execute it with the node program

> node example.js
Server running at http://127.0.0.1:8124/

Now you might be thinking that where to write (node example.js) command. If you start node.js and try to write

> node example.js



it will not work, instead you have to use cmd command. Its because Node.exe or Node.js is application that use to run the code from the file or to run the code which is written inside it. Its a node prompt and its same as typing node in cmd.


Please Like and Share the Blog, if you find it interesting and helpful.

No comments:

Post a Comment