What exactly is NodeJS? Is it a good idea to use NodeJS?

Introduction

NodeJS enables real-time web apps to use push technology over web-sockets. It provides real-time, two-way connections to web applications, allowing both the client and server to initiate communication and freely exchange data.

According to Wikipedia : “Node.js is an open-source, cross-platform, back-end JavaScript runtime environment that runs on the V8 engine and executes JavaScript code outside a web browser.”

The core idea of Node.js is to use non-blocking, event-driven I/O to remain lightweight and efficient in the face of data-intensive real-time applications running across distributed devices.

Node.js is a tremendously fast and high-quality Virtual Machine written by people like Lars Bak, one of the world’s best VM engineers (Virtual Machines).V8 is constantly updated and is one of the fastest interpreters for any dynamic language available today. As mentioned Node.js I / O (Input / Output) capabilities are light and powerful, allowing the developer to fully utilize the system’s I / O. TCP, DNS, and HTTP are all supported by Node. One of Node.js’s strengths is its ability to keep many connections open and on hold.

How NodeJS works ?

Node.js runs on a single thread and uses non-blocking I/O calls to support tens of thousands of concurrent connections in the event loop.

As it shares a single thread , developers must be extremely cautious not to allow an exception to bubble up to the core Node.js event loop, causing the Node.js instance to terminate.

The V8 JavaScript Engine

The JavaScript engine that powers Google Chrome is known as V8. It’s the thing that runs our JavaScript while we’re browsing with Chrome. V8 provides the environment in which JavaScript runs. The browser provides the DOM and other Web Platform APIs.

V8 is written in C++, and it’s continuously improved. It is portable and runs on Mac, Windows, Linux and several other systems.

NPM (Node Package Manager)

NPM (Node Package Manager) is a package manager written entirely in JavaScript by Isaac Schlueter; using NPM, we can obtain any library with a single line of code, allowing us to add dependencies of Only, distribute packages, and effectively manage both the modules and the project to be developed in general.

You can also design your own packages and distribute them to the entire community.

npm is not the only Node package manager; there is also yarn, a Facebook-provided alternative whose main advantage is the download speed of the packages.

Useful npm modules

express — web development framework for Node.js

mongodb, mongojs — MongoDB wrappers to provide the API for MongoDB object databases in Node.js

moment — A JavaScript date library for parsing, validating, manipulating, and formatting dates.

connect — An extensible HTTP server framework for Node.js

How to use or execute a package installed using npm

If you want to install express

npm install express

To use it in your code, simply import it into your program

const express = require('express');

or

import express from "express";

npm dependencies and devDependencies

npm install react

from above code what we do is install react as a dependency, but

npm install nodemon --save-dev

this install nodemon as a devDependency

Development dependencies are meant to be development-only packages that will not be used in production. Consider testing packages, webpack, or Babel.

Use of NodeJS

Node.js is a scalable network application runtime that is designed as an asynchronous event-driven JavaScript runtime.

Many connections can be handled concurrently in the following example. The callback is fired with each connection, but if there is no work to be done, Node.js will sleep.

const http = require('http');  
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World');
});
server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); });

When to use NodeJS ?

SERVER-SIDE WEB APPLICATIONS

If your application does not require any CPU-intensive computation, you can write it entirely in JavaScript, even down to the database level if you use a JSON storage Object DB such as MongoDB. This significantly simplifies development .

Cons of NodeJS

Because Node.js is single-threaded, it may be a poor choice for web servers that also serve as computational servers, as heavy computation will stymie the server’s responsiveness. However, Node.js is not without merit: The technology is quite mature and widely used for a wide range of server types.

Why NodeJS so popular?

Aside from being effective, Node.js is popular due to its large, active, open-source, JavaScript-based ecosystem.

Furthermore, it does not significantly impair compatibility between versions.

Conclusion

Node.js has a distinct advantage in that millions of frontend developers who write JavaScript for the browser can now write server-side code in addition to client-side code without having to learn a completely new language.

Because of its single-threaded nature, Node.js is primarily used for non-blocking, event-driven servers. It’s used for traditional web sites and back-end API services, but it was built for real-time, push-based architectures.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

NGXS Facade

JavaScript Objects and its internal representation

Add a Progress Bar to Your Vue.js App with Vue-ProgressBar

How to Create a Navigation Bar With React-Router, Styled-Components, And Infrastructure-Components

React Router V6: How To Implement Routing In SPA

Chrome Extensions and Chrome Apps : a light review.

Understanding Reactive Native

Highlights of AngularConnect 2019

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kevinwijesooriya

Kevinwijesooriya

More from Medium

[Node.js] How to upload a file in node.js

Node Js For Web Development

8 Node.js Frameworks Every Developer Should Know [UPDATED]