Loading...

Node js Modules

Node js Modules

Introduction

The Node js runtime is based on Chrome’s V8 JavaScript engine. Node js is a lightweight and scalable network-driven app framework built on an asynchronous event-driven JavaScript runtime. The development of Node js applications can be readily scaled in both horizontal and vertical orientations. Both client-side and server-side apps are built with Node js. It has an open-source JavaScript runtime environment/model that allows single modules to be cached.

Node js is an open-source organization and it provides a runtime environment for running JavaScript code beyond your browser. It is not a framework neither it is a programming language. It is mostly used in the backend for building  API services, accessing databases, etc.

Why do developers use Node js?

Following are the reasons for choosing Node js over other backend languages:

  • Easy to learn as it uses JavaScript
  • Used for agile development and prototyping
  • Provides fast and scalable services
  • Asynchronous nature
  • Uses “Single-threaded-event-loop” architecture

Node js helps developers build large complex applications with ease by using microservices. It can handle thousands of requests coming to the server without slowing down the system. Using microservices in Node js, one can easily scale a large-scale system and can divide it into different chunks for feature updates. It helps add independent features to the application without changing other services.

Both tech stacks are being widely used for developing web apps. Large tech giants like Netflix and Instagram use this tech stack in their applications.

It is largely used by developers in the backend of an application. Due to its non-blocking I/O and asynchronous nature, it has become the primary language used on the server side. Tech giants like Netflix, PayPal, Linkedin, and Uber use it for building API and servers. 

Features:

  • API server
  • Data streaming
  • Microservices
  • Building Real-time applications

Modules in Node js

Modules in Node js are encapsulated code blocks that interface with an external application based on their relevant capabilities. Modules might consist of a single file or a collection of files/folders. Because of its reusability and ability to break down a difficult piece of code into manageable chunks, programmers are heavily reliant on modules. There are three types of modules in Node js as mentioned in this article. These modules collectively help build applications in Node js. Here are some important modules that you need to know if you are learning Node js or building an application.

List of modules in Node js

There are three sorts of modules:

  • Local Modules 
  • Core Modules
  • Modules from third parties

Core Modules: Node js comes with a number of built-in modules that are included with the platform. The required function can be used to load these modules into the program.

Syntax:

var module = require('module_name');
Code language: JavaScript (javascript)

Depending on what the module returns, the require() function will return a JavaScript type. The following example shows how to construct a web server with the Node js HTTP module.

var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/html'}); res.write('Welcome to Codedamn!'); res.end(); }).listen(3000);
Code language: JavaScript (javascript)

The need() function returns an object in the previous example because the HTTP module returns its functionality as an object. When someone tries to contact the computer on port 3000, the function http.createServer() is called. The status code for the res.writeHead() method is 200, which indicates that everything is fine, and a second argument is an object holding the response headers.

Some of the most important core modules of Node js are listed below:

HTTP– starts a Node.js HTTP server. assert- a collection of assertion routines for testing.

fs– is a file system handler.

path– is a collection of methods for dealing with file paths.

process– gives you access to and control over the current Node.js process.

os– is a command that displays information about the operating system.

querystring– is a program that parses and formats URL query strings.

URL– module contains utilities for resolving and parsing URLs.

Local Modules: Local modules are developed locally in your Node.js application, unlike built-in and external modules. Let’s make a simple calculating module that performs a variety of calculations like mathematics etc. These are also known as in-built modules of Node js. These modules get updated along with Node js latest version.

Third-party modules

These are modules that can be found online and installed using the Node Package Manager (NPM). These modules can be installed locally or globally in the project folder. Mongoose, express, angular, and react are some of the most popular third-party modules.

Example:

npm install express npm install mongoose npm install -g @angular/cli
Code language: JavaScript (javascript)

Conclusion

Hope you have understood about modules in Node js. If you have any queries or doubts regarding modules in Node js, do drop a text in the comment section. If you want to learn Node js, check out Codedamn courses and the developer section if you like reading articles. Do join our community at Codedamn!

Sharing is caring

Did you like what mansi wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far