Feb 12, 2014

First HelloWorld in Node.js



To create our first HelloWorld in Node.js the first thing that we need is to install Node.js on our machine, if you don't have it, visit this post:


Now, we can create our first HelloWorld!!!!!





"HelloWorld!" in Console


Create our first HelloWorld in console is very easy, just follow a few simple steps:


          - Open your favorite JavaScript editor: Sublime Text, NotePad + +, Dreamweaver, etc..

          - Write:     console.log (" This is my first HelloWorld ");  

          - Save it with the JavaScript extension (helloworld.js).

          - Open your terminal.

          - Locate the folder where we saved the file.

          - Write in terminal:   $ node helloworld.js 





Now, we can see our message "This is my first HelloWorld!" in terminal. Do you want to see the message in the browser?




"HelloWorld!" in Browser


Now we will create a helloworld that we will see from the browser, it is very easy too. you need to follow this steps:


          - Open your favorite JavaScript editor: Sublime Text, NotePad + +, Dreamweaver, etc..

          - Write: 


// Load the http module to create an http server.

   var http = require('http');

 // Configure our HTTP server to respond with Hello World to all requests.

   var server = http.createServer(function (request, response) {
   response.writeHead(200, {"Content-Type": "text/plain"});
   response.end("Hello World"); });

 // Listen on port 3000, IP defaults to 127.0.0.1 ( Localhost )

   server.listen(3000);

 // Put a friendly message on the terminal

   console.log("Server running at http://127.0.0.1:3000/");


          - Save it with the JavaScript extension (helloworld2.js).

          - Open your terminal.

          - Locate the folder where we saved the file.

          - Write in terminal:   $ node helloworld2.js 

          - Open the Browser with this url: http://127.0.0.1:3000/



Now, we can see our message "This is my first HelloWorld!" in Browser.


1 comment:

  1. I don't have any idea about programming (only C++) but I think that your blog it is a good opportunity to learn, because it's so clear!

    I will recommend this blog to my friends!

    Congratulations!

    ReplyDelete