Introduction To Node.js

Introduction

Node.js is a server side JavaScript platform which is built on Google Chrome’s JavaScript V8 engine. It is an open source and cross platform application to develop server side and networking application. Anyone can develop the Node.js application by written code in JavaScript and it can be run on Microsoft Windows, Linux or OS X.

Node.js is not a new language and also it is not just framework built on JavaScript. It is built on Chrome's JavaScript Runtime, so the code is written, execute very similarly to browser.

Features of Node.js

Following are some important feature of Node.js

  • Fast in Code execution: It is very fast in code execution.

  • Highly scalable: Node.js uses single thread model for event looping. Events are responded to the server without blocking other operation. This makes the Node.js highly scalable. Traditional servers Create limited threads to handle request and Node.js Creates single thread that provide service to much larger number of requests.

  • Event Driven and Asynchronous: All API of Node.js is asynchronous. It means that server is moving the next API call without wait for returning data of previous request.

  • No Buffering: Node.js is never buffering any data.

Many of us are confused about Node.js. It is not a web server like Apache. Node.js is providing new way to execute our code. It is JavaScript runtime. Node.js is provides facility to create HTTP server and we can host our application on same.

Node.js Installation

First step is to download the Node.js and installed it. Node.js installation required at least Windows Vista If we have windows machine.

Working with Node.js

Ones we have installed Node.js, we can start Node.js using command called "node".We have two options to run the node command. The first is command without any arguments. It will open shell and we can execute the JavaScript code by raw.



In the above example, I typed "console.log('Hello C# corner')" into the shell, Node will execute this code provided the output. Node is also printing "undefined" at end. Node is always return value of each command and console.log function doesn’t return anything.

Another way to run Node is by providing a JavaScript file to execute.



In this example, I have created "Test.js" file into drive and move code Console.log('Hello C# corner') code to this file. Load this file using node command. I have pass file name as command argument. Node executes the JavaScript code defines inside the files.

REPL (Read Eval Print Loop)

The REPL stands for Read Eval Print Loop that is simple program that accepts the commands, evaluates the commands and print their results. It represents a computer environment like Unix/Linux shell or a window console in which we can enter the command and system responds with output. REPL performs the following tasks:

  • READ - Read the input from user, parse it into JavaScript data structure and stored it in memory.
  • EVAL - Execute the data structure
  • PRINT- Print the result
  • LOOP - Loop the command until user press Ctrl+C two time.

Node.js is very useful in following types of applications

  • SPA (Single Page Applications)
  • Application that deal with JSON
  • Applications use I/O Bound
  • Data Streaming Applications
  • DIRT (Data Intensive Real time) Applications

Installing Node.js Modules with NPM

NPM stands for Node.js Package Management. It comes with Node.js platform and allows us to install various packages for Node.js. This package manager supports various command to install and remove the modules. Here one important note is we required either package.json file or the node_modules folder to install modules locally.

One of the best things about npm is that it locally stores all dependencies. For example, if module X use module A version 1.0 and module Y use module A version 1.5 then both X and Y will have their own local copies of module A.



The above command will create node_modules folder in the current drive if it does not exist and will download the package into this folder.

For example, I want to install a package called lodash, so using following command we can download lodash module.



We can verify the download by using "dir" command (for windows) or "ls" command (for Unix system).



It will also notify us when installed package failure.



Summary

In this article, we learn what Node.js is, Features of Node.js, what use of Node.js is and how to download the modules using npm in Node.js.


Similar Articles