A Comprehensive Overview of Node.js

A Comprehensive Overview of Node.js

Day 38 of 60 : Mastering Node.js Fundamentals

What is Node.js :

  • Node.js is a cross-platform runtime environment and library for running JavaScript applications outside the browser.

  • It is used for creating server-side and networking web applications.

  • It is open source and free to use.

  • Many of the basic modules of Node.js are written in JavaScript.

  • Node.js is mostly used to run real-time server applications.

  • Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.?

  • Node.js also provides a rich library of various JavaScript modules to simplify the development of web applications.

    1. Node.js = Runtime Environment + JavaScript Library

Different parts of Node.js :

The following diagram specifies some important parts of Node.js:

what is nodejs

Features of Node.js :

Following is a list of some important features of Node.js that makes it the first choice of software architects.

  1. Extremely fast: Node.js is built on Google Chrome's V8 JavaScript Engine, so its library is very fast in code execution.

  2. I/O is Asynchronous and Event Driven: All APIs of Node.js library are asynchronous i.e. non-blocking. So a Node.js based server never waits for an API to return data. The server moves to the next API after calling it and a notification mechanism of Events of Node.js helps the server to get a response from the previous API call. It is also a reason that it is very fast.

  3. Single threaded: Node.js follows a single threaded model with event looping.

  4. Highly Scalable: Node.js is highly scalable because event mechanism helps the server to respond in a non-blocking way.

  5. No buffering: Node.js cuts down the overall processing time while uploading audio and video files. Node.js applications never buffer any data. These applications simply output the data in chunks.

  6. Open source: Node.js has an open source community which has produced many excellent modules to add additional capabilities to Node.js applications.

  7. License: Node.js is released under the MIT license.

Node.js Console :

The Node.js console module provides a simple debugging console similar to JavaScript console mechanism provided by web browsers.

There are three console methods that are used to write any node.js stream:

  1. console.log()

  2. console.error()

  3. console.warn()

Node.js console.log() :

The console.log() function is used to display simple message on console.

  1. console.log('Hello Student');

Open Node.js command prompt and run the following code:

  1. node console_example1.js

Ourput: Hello Student

Node.js console.error() :

The console.error() function is used to render error message on console.

  1. console.error(new Error('Hell! This is a wrong method.'));

Open Node.js command prompt and run the following code:

  1. node console_example3.js

Node.js console example 3

Node.js console.warn()

The console.warn() function is used to display warning message on console.

File: console_example4.js

  1. const name = 'John';

  2. console.warn(`Don't mess with me ${name}! Don't mess with me!`);

Open Node.js command prompt and run the following code:

  1. node console_example4.js

Node.js console example 4

Node.js REPL

  • The term REPL stands for Read Eval Print and Loop.

  • It specifies a computer environment like a window console or a Unix/Linux shell where you can enter the commands and the system responds with an output in an interactive mode.

REPL Environment

  • The Node.js or node come bundled with REPL environm/ent.

  • Each part of the REPL environment has a specific work.

  1. Read: It reads user's input; parse the input into JavaScript data-structure and stores in memory.

  2. Eval: It takes and evaluates the data structure.

  3. Print: It prints the result.

  4. Loop: It loops the above command until user press ctrl-c twice.

    How to start REPL

You can start REPL by simply running "node" on the command prompt. See this:

node.js repl 1

You can execute various mathematical operations on REPL Node.js command prompt:

Node.js Simple expressions :

After starting REPL node command prompt put any mathematical expression:

  1. Example: \>10+20-5

  2. 25

node.js repl 2

  1. Example2: \>10+12 + (5*4)/7

node.js repl 3

Using variable :

  • Variables are used to store values and print later.

  • If you don't use var keyword then value is stored in the variable and printed whereas if var keyword is used then value is stored but not printed.

  • You can print variables using console.log().

Example:

node.js repl 4

Node.js Multiline expressions

Node REPL supports multiline expressions like JavaScript. See the following do-while loop example:

var x = 0  

undefined  

> do {  

... x++;  

... console.log("x: " + x);  

... } while ( x < 10 );

node.js repl 5

Node.js Underscore Variable :

You can also use underscore _ to get the last result.

Example:

node.js repl 6

Node.js REPL Commands :

CommandsDescription
ctrl + cIt is used to terminate the current command.
ctrl + c twiceIt terminates the node repl.
ctrl + dIt terminates the node repl.
up/down keysIt is used to see command history and modify previous commands.
tab keysIt specifies the list of current command.
.helpIt specifies the list of all commands.
.breakIt is used to exit from multi-line expressions.
.clearIt is used to exit from multi-line expressions.
.save filenameIt saves current node repl session to a file.
.load filenameIt is used to load file content in current node repl session.

Node.js Exit REPL:

Use ctrl + c command twice to come out of Node.js REPL.

node.js repl 7

What Next?

Node.js Package Manager