Node JS hello World Quick Tutorial

Why use Node.js ?

If you have been working on the web for a while, you definitely had to use JavaScript at some point. Now, if you have been using JavaScript for the front-end, we need to switch to some other language when working on the backend. This switching might be tiring or confusing. Node is literally a server-side language using JavaScript. That means that both the front-end and the back-end can be created using JavaScript, eliminating the switching hassle.  So, especially if you enjoy working with JavaScript, Node is for you. Node has been built on JavaScript since it has closures and first-class functions which makes node allowing strong event-driven programming. In fact, Node is event-driven and thus the flow of execution is determined by the events handled by event handlers and callbacks allowing for asynchronous programming.

Installing Node.js

Visit the node.js website and click the “Download Button” under the big Install Button.

1.download

Depending on what type of Operating System you are using, select the appropriate type. In my case, I have a windows 64-bit, so I have downloaded the first (msi) download:

2.download-type

When download is complete, run the file and keep clicking “next” until installing starts. Click Finish when install is complete.

Testing Node

Lets just make sure Node is successfully installed.

To launch node – Open the cmd (command-line) and type:

node                                                       –             this is the command given to launch node. A new line should appear with the symbol “>” as shown below

3.node

To close node you will need to press “CTRL + C” and it will ask you to press “CTRL + C” again to exit. Once you press “CTRL + C” twice, node will exit.

4.node-quit

Running Hello World using Node

Now lets create a simple “Hello World” print using node. Open the cmd (command-line) and type:

mkdir HelloWorldFolder             –            this will create a new folder named “HelloWorldFolder”

cd HelloWorldFolder                     –            this will change directory and enter the new folder “HelloWorldFolder”

Now open the new created folder in windows form if you feel more comfortable and create a file name “test.js”

Open the new file using a simple text editor and paste only the following to display Hello World on-screen:

// Call the console.log function.
console.log(“Hello World”);

notepad

Back to command line to run the js file and type:

node test.js                                      –             this will run the file “test.js”  in node

As you can see below node is running the js file and printing “Hello World” on the screen.

5.run-node

Pretty quick and easy.. enjoy 😉