Node

Node.js

Server side scripting based on Google’s V8 JavaScript engine.

Used to build scalable programs especially web applications that are computationally simple but are frequently accessed.

You can use Node.js in developing I/O intensive web applications like video streaming sites. You can also use it for developing: Real-time web applications, Network applications, General-purpose applications and Distributed systems.

Key features

  • Single-threaded but highly scalable system

  • JS as its scripting language

  • Asynchronous, event-driven I/O instead of separate processes or threads.

  • High output via single-threaded event loop and non-blocking I/O.

  • Makes building scalable network programs easy

  • Generally fast

  • It almost never blocks

  • It offers a unified programming language and data type

  • Everything is asynchronous

  • It yields great concurrency

Why is Node.js Single-threaded?

By doing async processing on a single-thread under typical web loads, more performance and scalability can be achieved as opposed to the typical thread-based implementation.

When Should We Use Node.js?

When we are building systems which require high number of I/O operations. Like saving data to DB, files access. NodeJS shines in such situation.

Example: IoT Applications, Video Streaming.

When Node.js should not be used?

When you have lot of synchronous code that needs to be run.

Applications which require number crunching or data analysis should not use Node.js.

Example: Matrix Multiplication, Summation, Aggregation of large datasets. In such cases, applications which allow Multi Threading out of the box like Python, Java should be used

Pros and Cons

Pros:

  • If your application does not have any CPU intensive computation, you can build it in Javascript top to bottom, even down to the database level if you use JSON storage object DB like MongoDB.

  • Crawlers receive a full-rendered HTML response, which is far more SEO friendly rather than a single page application or a websockets app run on top of Node.js.

Cons:

  • Any intensive CPU computation will block node.js responsiveness, so a threaded platform is a better approach.

  • Using relational database with Node.js is considered less favourable

REPL

Read Eval print Loop.

In Node.js is used to execute ad-hoc Javascript statements. The REPL shell allows entry to javascript directly into a shell prompt and evaluates the results. For the purpose of testing, debugging, or experimenting, REPL is very critical.

Types of functions in Node.js

Blocking functions

In a blocking operation, all other code is blocked from executing until an I/O event that is being waited on occurs. Blocking functions execute synchronously

For example: const fs = require('fs'); const data = fs.readFileSync('/file.md'); // blocks here until file is read console.log(data); // moreWork(); will run after console.log

The second line of code blocks the execution of additional JavaScript until the entire file is read. moreWork () will only be called after Console.log

Non-blocking functions

In a non-blocking operation, multiple I/O calls can be performed without the execution of the program being halted. Non-blocking functions execute asynchronously.

For example:

const fs = require('fs'); fs.readFile('/file.md', (err, data) => { if (err) throw err; console.log(data); }); // moreWork(); will run before console.log

Since fs.readFile () is non-blocking, moreWork () does not have to wait for the file read to complete before being called. This allows for higher throughput.

Streams

Objects that allow reading of data from the source and writing of data to the destination as a continuous process.

4 types of streams.

  • to facilitate the reading operation

  • to facilitate the writing operation

  • to facilitate both read and write operations

  • is a form of Duplex stream that performs computations based on the available input

Functionalities of NPM in Node.js

NPM (Node package Manager) provides 2 functionalities:

  • Online repository for Node.js packages

  • Command line utility for installing packages, version management and dependency management of Node.js packages

Last updated