Introduction to NodeJS

 

      “Hi there, I am using Whatsapp” -- You might have seen this in your whatsapp contacts’ statuses. You can chat, talk and now even video call with your friends using whatsapp. Every wondered how whatsapp actually works? What happens after you click the send button? 

Well, once you click the send button, your message travels from your phone, to a chain of servers (computers which process your message) and send it to your friend’s phone. Now the servers are an important part of any network. They are the brains of the network. It is responsible for sending the message to correct phone and in a secure way. 

Servers are everywhere. Yes everywhere. Even your PC or mobile is a server on its own. Many technologies are available at the present which enable us to build web servers for every purpose. But somewhere developers felt that these technologies like Ruby on Rails or Django do not offer much required support for high number of concurrent connections nor they were fast enough when under heavy traffic. This is where the need for developing a new platform came from and this way NodeJS was born.

In most simplistic terms , servers are processes running in a machine which accepts incoming requests and gives out some responses for particular request. Servers are the backbone of every website, web app etc. They are responsible for sending the web pages to the user, authenticate users, query database, display results etc. 

NODEJS

“NodeJS is a runtime environment which runs JavaScript on a system as a server without the need for a web browser. “

   Now NodeJS is not a separate language. It is a runtime environment i.e. a separate ecosystem in your PC which enables you to run JavaScript as a process. This is contrary to the previous use of JavaScript where it used to run only in web browsers and helped interaction between user and the web page. 

With NodeJS, you can use JavaScript to write

  • Servers
  • Command Line Scripts
  • Desktop Applications
  • Mobile Applications

Yes.. Everything with simple JavaScript using NodeJS. It makes your life so easy.

     NodeJS is a runtime environment which means that all the scripts you write will run inside a single NodeJS process. Of course the scripts can access the external resources using various APIs provided by NodeJS. 

     Adding to your convenience, NodeJS also provides you with a bunch of libraries -- or modules which contains many functions like copying files, deleting files, making more processes etc. Moreover you can create your own module and publish it on the web which allows you to share your module with other people. You can search for modules on http://www.npmjs.com. At the time of writing this, there are around 3,78,202 NodeJS module available for you to use. That combined with the young age of NodeJS, it comes to an average of around 423 modules per day being uploaded on npmjs. This is higher than the number of modules/libraries available for Java, Ruby and PHP combined. So yeah, the developer support is immense.

Before starting to write any NodeJS scripts, we first need to install NodeJS and setup an environment for it to function properly.

Environment Setup for Windows OS :

      Goto the url https://nodejs.org/en/download/current/ and download the latest installation for your system. Download and install Windows Installer (.msi) [32bit or 64bit depending on your system] from the above link and you will have the latest version of NodeJS with NPM running on your system.
Click the icon to begin installation


Click Next


Again Click Next


Again Next
Finally click Install..


It will start installing… 


And finally NodeJS has been installed. 

Setting Environment Variables for Windows OS
         Mainly we will be using NodeJS for building web servers.  We need to convey this information to our system that the current machine is a development machine and not a production one. For this you need to add an environment variable NODE_ENV=”development” in your system so that the server knows that this system is a development environment and not for production.

1. Open Control Panel


2. Click System


3. Click Advanced System Settings
4. Go to Advanced tab


5. Click Environment variables… button



6. Click new and Add the variable NODE_ENV with value development

Press OK to save and there you go. You are done saving the environment variable.

You are now set to write to your first NodeJS codes… 

Environment Setup for MacOS

Visit the url https://nodejs.org/en/download/current/ & Download and install macOS Installer (.pkg) from the above link.

Environment Setup for Linux OS (Ubuntu)

For linux based systems, there is a installation package which can be downloaded from https://nodejs.org/en/download/ . Just download and install the Linux binaries for your respective system and that is it. 

Other way is to install via package manager. Open your terminal and execute the following command:

$ sudo apt-get install -y build-essential
$ curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -
$ sudo apt-get install -y nodejs

This would install NodeJS and NPM on your local system.

Setting Environment Variables for Linux & MacOS:

1) Open .bash_profile file
$ vim ~/.bash_profile

2) Append the following
export NODE_ENV=development
          Note: There should be no space around the equality( = ) character.

3) Save the file and restart the terminal

Awesome. Now NodeJS is setup perfectly on your system.

No comments:
Write comments

Popular Posts