Node.js – Starter Guide For Complete Beginners

In this tutorial, you will learn the basics of Node.js, its installation, along with basic Node.js Hello World application. Before Getting started with Node.js, you must know what Node.js is. Many of the beginners take Node.js as a web server but, it is not a web server. It doesn’t act as Apache and other web servers and also can’t do anything by itself. If you want it to serve as a web server, you must write the server.

Writing a server? 

Don’t confuse yourself! It is not that much hard. There is a wide range of built-in libraries which can help you to write a server.

Introduction

According to the official Node.js website, Node.js is JavaScript runtime. It is built on Google Chrome’s JavaScript V8 Engine. Node.js is Based on Event-driven and non-blocking I/O which make it efficient and light weight as compared to other platforms. It provides another way to write servers with the help of JavaScript.

Node.js uses libuv which is a multi-platform supported library and can do asynchronous I/O. Node.js if alone is single threaded but libuv makes it multi-threaded.

The simplest introduction to Node.js is that it is a way to run code on your computer, you can also call it JavaScript runtime.

 Node.js Features

Many tremendous features of Node.js are behind its popularity.

  • As because it is built on Google Chrome’s V8 JavaScript Engine, so it has excellent speed.
  • The asynchronous functionality of Node.js makes it non-blocking, this asynchronous behavior means that it don’t wait for the API to respond. After calling an API, it moves to the next API and when the previous API responds it is notified.
  • When using Node.js, the applications will never do any buffering if you don’t want; It will keep on displaying data in small chunks.
  • Node.js is single threaded with event looping. Event looping mechanism makes it highly scalable due to which it is much better than Apache HTTP server. The non-blocking feature makes it better than other servers.

Node.js Installation Guide

You can install Node.js like any other software. Just get the installer for your operating system from official Node.js download page and install. This installer will install a new command line tool for Node.js.

For UNIX/Linux/MAC OS X and SunOS

Download the required package and extract in the /usr/local/nodejs directory and set the environmental variable PATH to /usr/local/nodejs/bin

Update npm (Node Package Manager)

You can download updated Node Package Manager from here.

Some useful Command you will need

To check the npm version write

Node Express

To install Express

and to include node express

to uninstall node express

to update node express

 

Say Hello to Node.js Command Line

Just to test the Node.js terminal write a simple Hello World Command.

Pressing Enter will display your Hello World message. The output of the above code will be

Hello to Node.js
undefined

Undefined is displayed due to no return type of console.log(). It always displays the return value of each command.

Node.js Terminal
Node.js Terminal

To check for the return value lets consider a simple example to add two variables to the console. After initializing each variable, the variable value will be returned.

Will return 10 after pressing Enter

again press Enter and the command will return 20

x+y will return the sum of both the variables.

Testing Node.js command line return
Testing Node.js command line return

 

Running a JavaScript File

  1. To create your first real Node.js Hello World file. So, move console.log(‘Hello to Node.js’) to a JavaScript file.
  2. Create a new file in the text editor i.e. Notepad++, Sublime Text or any other editor. Place console.log(‘Hello to Node.js’) into the file and save it with .js extension.
  3. Open Command Prompt, browse to the directory where you saved your JavaScipt file and run the file by using node command. I saved the file on Desktop and named it as main.js. To run the file write node main.js
  4. See the output on your console.
Node.js Hello World Project in Command Prompt
Node.js Hello World Project in Command Prompt

Creating First Node.js

Before you create your first project in Node.js, you must understand the building blocks of Node.js

Components of Node.js

Imports

Like in other languages when you want the required libraries or classes, there are specific statements, in Node.js you will use require() command to load a specific module. For example

Create a Server

You must create a server that should listen to the client’s requests, similar to Apache HTTP server. The server should listen to the client requests and respond to them

You will use the HTTP.createServer(function(request,response){ <do something> }).listen(<port>) method to create the server

 

Complete Code For main.js

 

Running Node.js Hello World Application

In the command prompt run the main.js file to start the server you created.

It will start running the server you created and will display Server running at http://127.0.0.1:8081/.

Running Hello World Server
Running Hello World Server

Now make a request to the newly created server from the browser by http://127.0.0.1:8081/ and the browser will respond by the Hello Message i.e. Hello to Node.js

Node.js Server Request Response
Node.js Server Request Response

 

You have now successfully created Node.js Hello world Server. Hope that you like this tutorial. Stay tuned for more upcoming tutorials.

Stay Blessed!