Tutorials

Soft Skills

Node.js Introduction & Environment Setup

“NodeJS is a runtime environment which enables us to run JavaScript without any Web Browser. “

WWhhoooosshhh…..!!!!!!!! Too many complex terminologies…!!! ¯\_(⊙︿⊙)_/¯Let us see things one by one.

JavaScript:

JavaScript is one of the many scripting languages (languages that a computer can understand) which allows us to make computers do what we want them to do. And Yes!! You can make JavaScript make you some food.

JavaScript was originally meant to be run on Web Browsers (The thing in which you are viewing this) -- Google Chrome, Firefox, Internet Explorer etc etc and was to be a client side functionality i.e. it would run on your local web browser only. Once you close your browser, JS ceases to exist.

Runtime Environment

It is a state of target machine which enables you to run some program by providing certain libraries and modules to interact with the OS.

Now adding all together, we get : NodeJS is a set of libraries and modules which help us (or the system) to run JavaScript on the machine directly without the need for any web browser.

Q. But why did you tell me this? ¯\_(ツ)_/¯

Now since you can run JavaScript on a system without a browser, you can surely run web servers on JavaScript.

Q. Why would I need to use JavaScript for server when there is PHP, Ruby on Rails, Python etc etc?

Due to single threaded, event driven, non-blocking architecture of JavaScript, NodeJS servers tend to be amazingly fast and can handle a lot more concurrent connections as compared to traditional servers like PHP and Rails . Also with the increasing popularity of JavaScript in the past 4 years, the community support for NodeJS modules have increased exponentially and surpass the module count of other web server technologies as evident from the graph:

Environment Setup

Installation

For windows users

Visit the url 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.

For MacOS

Visit the url & Download and install macOS Installer (.pkg) from the above link and you will have the latest version of NodeJS with NPM running on your system.

Setting environment variables

This tutorial will focus on building a web server with NodeJS. So for this one more step is recommended. 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.

Linux & MacOS:

1) Open .bash_profile file

$ nano ~/.bash_profile

Append the following

export NODE_ENV=development

Note: There should be no space around the space character3) Save the file and restart the terminal

You would need to use an text editor for writing JavaScript codes. There are many text editors to choose from.

WebStorm: https://www.jetbrains.com/webstorm/

Sublime Text: https://www.sublimetext.com/

Atom: https://atom.io/

And many more

I would recommend using WebStorm. It is a licenced software but can be used by students for free for educational purposes. Sublime and Atom also work as a charm for writing codes.

Download the installation files for your favorite text editor and install them. All of these have one click installation files. No need for any terminal commands.

Everything is set now. Let us start building.

Writing your first NodeJS Application

As is the convention, let's create an Hello world application.

In NodeJS console

Start the Node console by running

$ node

This will start the NodeJS terminal. Time to print “Hello World”. In the Node terminal, write

> console.log(“Hello World !!!”)

And there you go. You just wrote a program to print Hello World to the console. Press Ctrl+C twice or enter .exit to exit the terminal. Here is what the terminal would look like.

Explanation:

console.log(“Hello World !!!”) print the text “Hello World !!!” to the console as shown in line 3.

The return value of console.log() is undefined i.e. void . Hence the undefined in line 4.

Using JavaScript files:

Open up a new empty javascript file in your favorite text editor and name is something, say index.js. Write the following contents.

Enabling strict mode in JavaScript prevents us from writing insecure JavaScript codes. Insecure JavaScript Code is a piece of code which contains various illegal codes like using undeclared variables, delete functions, passing same arguments etc. These illegal codes can make debugging difficult in large applications. Enabling strict mode throws an error whenever illegal codes are encountered in the code.

process is a global object created by the NodeJS environment having various properties of the environment. (More on it later)

process.stdout refers to the standard output for the NodeJS process which here is the terminal.

process.stdout.write and console.log both print strings to the console. The main difference is that console.log appends a new line character at the end of the current line.

To run this script, open the terminal at the location of this file and start the script using

$ node index.js

You should see an output like: That is it. Your first application just ran successfully.