Types of Modules in Node JS

  • By Sarika Ganesh Kore
  • June 27, 2023
  • JavaScript
Types of Modules in Node JS

Types of Modules in Node JS

Types of Modules in Node JS is a set of functions that can be included in your program. Node JS has its own set of modules that can be included without installation. Every module has its own context so it cannot be mixed with another module. Each module can be placed in a separate js file. Unlock your full potential with comprehensive Full Stack classes in Pune. Gain expertise in front-end and back-end development, database management, and more. Enroll now!

If you are at a beginner level in Node JS do visit: Basics Of Node JS

Types of Modules in Node JS

Node js has the following 3 types of modules:-

  1. Core modules
  2. Local Modules
  3. Third-Party Modules

Types of Modules in Node JS

Core Modules:-

There are some modules in Node Js that are included with the installation of Node JS. These modules are included in the program using require function.

e.g. const abc=required(‘abc’)

The core modules which can be included in the program are as follows:-

  • http:- It creates http server in your node js program.

e.g. var http = require(‘http’);

http.createServer(function (req, resp) {

   resp.write(‘Hello Everyone!’);

   resp.end();

}).listen(5052); 

 

  • assert:- It creates a set of asserted functions that can be used for testing.

e.g. var assert = require(‘assert’);

assert(15 > 72);

  • fs:- It is used to handle the file system.

e.g. var http=require(‘http’)

var fs=require(‘fs’)

http.createServer(function(req,res)

{

    fs.readFile(‘intro.html’,function(err,data)

{

        res.writeHead(200,{‘Content-Type’:”text/html”})

        res.write(data)

        return res.end()

    })

}).listen(3011)

  • path:- This module have the methods to deal with file paths.

e.g. const express=require(‘express’)

const path=require(‘path’)

const app=express()

const pageaddr=path.join(__dirname,’public’)

// app.use(express.static(pageaddr))

app.use(”,(_,resp)=>{

    resp.sendFile(`${pageaddr}/index.html`)

})

app.use(‘/about’,(_,resp)=>{

    resp.sendFile(`${pageaddr}/about.html`)

})

app.use(‘/contact’,(_,resp)=>{

    resp.sendFile(`${pageaddr}/contact.html`)

})

app.use(‘*’,(_,resp)=>{

    resp.sendFile(`${pageaddr}/notfound.html`)

})

app.listen(5004)

  • process:- It provides the information of the process which is currently running in Node Js.

e.g. process.on(‘exit’, code => {

setTimeout(() => {

console.log(‘Will not get displayed’);

}, 0);

 

console.log(‘Exited with status code:’, code);

});

console.log(‘Execution Completed’);

 

Note: Learn to build dynamic, interactive user interfaces and master the popular React library. Enroll React JS Training in Pune Now

 

  • os:- It provides the information about the operating system.

e.g. var os = require(‘os’);

console.log(“Platform: ” + os.platform());

console.log(“Architecture: ” + os.arch());

 

  • querystring:- This module is used to parse and format URL query strings.

e.g. var querystring = require(‘querystring’);

var q = querystring.parse(‘year=2015&month=march’);

console.log(q.year);

 

  • url:- It is used to provide utilities to parse and handle url resolution.

e.g. var url = require(‘url’);

var adr = ‘http://localhost:8080/default.htm?year=2008&month=february’;

var q = url.parse(adr, true);

console.log(q.host);

console.log(q.pathname);

console.log(q.search);

var qdata = q.query;

console.log(qdata.month);

 

Local Modules:-

Local modules are created in your node application at the time of creating the application. 

e.g. exports.sum = function (num1, num2) {

return num1 + num2;

};

exports.subtr = function (num1, num2) {

return num1 – num2;

};

exports.multpl= function (num1, num2) {

return num1 * num2;

};

exports.divsn = function (num1, num2) {

return num1/ num2;

};

These exported functions can be used in another file using require() function like-

const calci = require(‘./calci’);

let num1=35, num2 = 30;

console.log(“Addition of 35 and 30 is “

+ calci.sum(num1, num2));

console.log(“Subtraction of 35 and 30 is “

+ calci.subtr(num1, num2));

console.log(“Multiplication of 35 and 30 is “

+ calci.multpl(num1, num2));

console.log(“Division of 35 and 30 is “

+ calci.divsn(num1, num));

 

Third-Party Modules:-

The modules which can be included in the program using NPM(Node Package Manager) online are called as third-party modules. We can include these modules globally or in the project folder. Third-party modules are:-

  • Express
  • Moongose
  • React
  • Angular

These modules are included using the command –

npm install express

npm install mongoose etc.

Do watch our latest video: Click Here

Author:-

Sarika Ganesh Kore

Call the Trainer and Book your free demo Class For JavaScript Call now!!!
| SevenMentor Pvt Ltd.

© Copyright 2021 | SevenMentor Pvt Ltd.

Submit Comment

Your email address will not be published. Required fields are marked *

*
*