NodeJs

Photo by Eli Francis on Unsplash

NodeJs

What is NodeJs?

Yes, JavaScript Runtime Environment.

Nodejs is a run-time environment🌎 that includes everything you need to execute a program written in Javascript. As an asynchronous event-driven JavaScript runtime, Node.js is designed to build scalable network applications🛠.

Node.js is open-source: This means that the source code for Node.js is publicly available. And it's maintained by contributors from all over the world. The Node.js contribution guide/ shows you how to contribute.

Node.js is cross-platform: Node.js is not dependent on any operating system software. It can work on Linux, macOS, or Windows.

Node.js is a JavaScript runtime environment: When you write JavaScript code in your text editor, that code cannot perform any task unless you execute (or run) it. To run your code, you need a runtime environment.

Browsers like Chrome and Firefox have runtime environments. That is why they can run JavaScript code. Before Node.js was created, JavaScript could only run in a browser. And it was used to build only front-end applications.

Node.js provides a runtime environment outside of the browser. It's also built on the Chrome V8 JavaScript engine/. This makes it possible to build back-end applications using the same JavaScript programming language you may be familiar with.

Ok but if you ever worked on the front-end side we have Reactjs and AngularJs where we also run your code in the browser so, what's the difference between the browser and Nodejs runtime environment let's see🐱‍🏍

  1. Access to the DOM APIs

    With the browser runtime, you can access the Document Object Model (DOM). And you can perform all the DOM operations. But Node.js does not have access to the DOM.

    Node.js exposes almost all the system resources to your programs. This means you can interact with the operating system, access the file systems, and read and write to the files. But, you do not have access to operating systems and file systems from the browser.

  2. Window vs Global object

    JavaScript has a built-in global object. The JavaScript global object for the browser is called the window object. In Node.js, the global object goes by the name global.

    The window object contains methods and properties available only in the browser environment.

  3. Control over runtime versions

    With Node.js, you can choose which version to run your server-side application on. As a result, you can use modern JavaScript features without worrying about any version-specific inconsistencies.

    Contrast this to the browser runtime environment. As a developer, you have no control over the version of browsers your clients use to access your app.

  4. Loading modules (import vs require keywords)

    Node.js offers out-of-the-box support for CommonJS and ES modules. You can load modules using the require keyword (CommonJS syntax) and the import keyword (ES syntax).

    Some modern browsers support ES modules. This means you can use import ES modules. However, you will still need to create bundles to cater to older browsers that do not support ES modules.

Blocking I/O model (synchronous) OR multi-threaded:

It refers to the blocking of further operations until the current operation finishes. Blocking methods are executed synchronously. Synchronously means that the program is executed line by line. The program waits until the called function or the operation returns.

Non-blocking I/O model(asyn) OR single threaded:

It refers to the program that does not block the execution of further operations. Non-blocking methods are executed asynchronously. Asynchronously means that the program may not necessarily execute line by line. The program calls the function to move to the next operation and does not wait for it to return.

Source: Geeksforgeeks, freecodecamp