0% found this document useful (0 votes)
11 views

Unit 3

Uploaded by

jayvaghela1542
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Unit 3

Uploaded by

jayvaghela1542
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Unit-3

Node.js Modules

In Node.js, Modules are the blocks of encapsulated code that communicate with
an external application on the basis of their related functionality. Modules can be a
single file or a collection of multiple files/folders. The reason programmers are heavily
reliant on modules is because of their reusability as well as the ability to break down a
complex piece of code into manageable chunks.
We will discuss the different types of Node.js Modules:
Table of Content
• Core Modules
• Local Modules
• Third-party modules

Core Modules
Node.js has many built-in modules that are part of the platform and come
with Node.js installation. These modules can be loaded into the program by using
the required function.

Syntax:
const module = require('module_name');
The require() function will return a JavaScript type depending on what the particular
module returns. The following example demonstrates how to use the Node.js http
module to create a web server.

const http = require('http');


http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write('Welcome to this page!');
res.end();
}).listen(3000);

In the above example, the require() function returns an object because the Http module
returns its functionality as an object. The function http.createServer() method will be
executed when someone tries to access the computer on port 3000. The res.writeHead()
method is the status code where 200 means it is OK, while the second argument is an
object containing the response headers. The following list contains some of the
important core modules in Node.js:

1
Shri V J Modha College of IT
Unit-3
Core Modules Description

http creates an HTTP server in Node.js.

set of assertion functions useful for testing.


assert

fs used to handle file system.

path includes methods to deal with file paths.

process provides information and control about the current Node.js process.

os provides information about the operating system.

querystring utility used for parsing and formatting URL query strings.

url module provides utilities for URL resolution and parsing.

2
Shri V J Modha College of IT
Unit-3
Local Modules

Unlike built-in and external modules, local modules are created locally in your Node.js
application. Let’s create a simple calculating module that calculates various operations.
Create a calc.js file that has the following code:
JavaScript
// Filename: calc.js

exports.add = function (x, y) {


return x + y;
};

exports.sub = function (x, y) {


return x - y;
};

exports.mult = function (x, y) {


return x * y;
};

exports.div = function (x, y) {


return x / y;
};

Since this file provides attributes to the outer world via exports, another file can use its
exported functionality using the require() function.

// Filename: index.js

const calculator = require('./calc');

let x = 50, y = 10;

console.log("Addition of 50 and 10 is "


+ calculator.add(x, y));

console.log("Subtraction of 50 and 10 is "


+ calculator.sub(x, y));

console.log("Multiplication of 50 and 10 is "

3
Shri V J Modha College of IT
Unit-3
+ calculator.mult(x, y));

console.log("Division of 50 and 10 is "


+ calculator.div(x, y));

Step to run this program: Run the index.js file using the following command:
node index.js

Output:
Addition of 50 and 10 is 60
Subtraction of 50 and 10 is 40
Multiplication of 50 and 10 is 500
Division of 50 and 10 is 5
Note: This module also hides functionality that is not needed outside of the module.

4
Shri V J Modha College of IT
Unit-3
Third-party modules
Third-party modules are modules that are available online using the Node Package
Manager(NPM). These modules can be installed in the project folder or globally. Some
of the popular third-party modules are Mongoose, express, angular, and React.
Example:
• npm install express
• npm install mongoose
• npm install -g @angular/cli

Example: Installing and Using Express


npm install express

Create a Simple Express Server:


const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => {


res.send('Hello World!');
});

app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});

Run the Server:


node server.js
When you navigate to http://localhost:3000 in your browser, you should see Hello
World!.

5
Shri V J Modha College of IT

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy