Basic NodeJS

Node is a cross-platform JS runtime build on top of Google's V8 engine which help us run JS outside browser.

Other JS runtime include Deno, which is created by the same creator of node but in rust.

<aside> 📔 A Node.js app runs in a single process, without creating a new thread for every request. Node.js provides a set of asynchronous I/O primitives in its standard library that prevent JavaScript code from blocking and generally, libraries in Node.js are written using non-blocking paradigms, making blocking behavior the exception rather than the norm.

</aside>

<aside> 📔 The "shebang" (#!) is the first line in the file, and tells the OS which interpreter to use for running the script by explicitly giving the absolute path of interpreter like #!/usr/bin/env node for JS.

</aside>

Pros and Uses

Single-threaded, based on event driven, non-blocking I/O model

Perfect for building fast and scalable data-intensive apps

Used for creating API with database behind it (preferably NoSQL), Data streaming (like YouTube), Real-time chat application and Server-side web application etc.

Cons

Shouldn’t be used for Applications with heavy server-side processing (CPU-intensive).

Browser VS Node

Both the browser and Node.js use JavaScript as their programming language but building apps that run in the browser is a completely different thing than building a Node.js application.

In the browser, most of the time what you are doing is interacting with the DOM, or other Web Platform APIs like Cookies. Those do not exist in Node.js. So you don't have the documentwindow and all the other objects that are provided by the browser similarly in the browser, we don't have all the nice APIs that Node.js provides through its modules, like the filesystem access functionality.

Node.js supports both the CommonJS and ES module systems (since Node.js v12), while in the browser we are starting to see the ES Modules standard being implemented. This means that you can use both require() and import in Node.js, while you are limited to import in the browser.

JS Engine

JavaScript engine parses and executes JavaScript code. The cool thing is that the JavaScript engine is independent of the browser in which it's hosted.

There are a lot of JS Engines which implement the ECMA ES-262 standard, also called ECMAScript, the standard used by JavaScript****:

JavaScript is generally considered an interpreted language, but modern JavaScript engines no longer just interpret JavaScript, they compile it. JavaScript is internally compiled by V8 with just-in time (JIT) compilation to speed up the execution.

Modules in node

A module is an encapsulated and reusable chunk of code that has its own context.

In Node every file is treated as a separate module.

When we work on a project we can use core modules (built-in), our own modules (local), 3rd party modules(npm).

Some important build-in modules in node are fs, path, os, events, http, childprocess, ws, stream etc.

To know about any 3rd party module/package use it's documentaion.

With Node as a backend language we can create a backend app which will listen for the request at a specific port and then process the request and send back a response.