SlideShare uma empresa Scribd logo
1 de 14
Prepared By:
Prof. Bareen Shaikh
Department of Computer
Science,
MIT ACSC,ALNDI(D), PUNE
NodeJs Modules
Topics
 3.3 Module and ModuleTypes
 3.4 Core Module, Local Module
 3.5 Directories as module
 3.5 Module. exports
Module and Module Types
 Module in Node.js is a simple or complex functionality organized in
single or multiple JavaScript files which can be reused throughout the
Node.js application.
 Each module in Node.js has its own context, so it cannot interfere with
other modules or pollute global scope.
 Each module can be placed in a separate .js file under a separate folder.
 Node.js ModuleTypes: Node.js includes three types of modules:
1. Core Modules
2. Local Modules
Core Module
 Node.js is a light weight framework.
 The core modules include bare minimum functionalities of Node.js.
 These core modules are compiled into its binary distribution and load
automatically when Node.js process starts.
 Need to be import the core module first in order to use in an
application.
 Following lists some of the important core modules in Node.js
 http
 url
 util
 path
 fs
Loading Core Module
 In order to use Node.js core or NPM modules, need to import it
using require() function as shown below.
var module = require('module_name');
 require() function return an object, function, property or any other
JavaScript type, depending on what the specified module returns.
Eg.
var http = require('http');
var server = http.createServer(function(req, res){
//write code here
}); server.listen(5000);
Local Module
 Local modules are modules created locally by user in Node.js
application.
 Modules may include different functionalities of application in
separate files and folders.
 These module can also package it and distribute it via NPM, so that
Node.js community can use it.
 For example,
If an application need to connect to MongoDB and fetch data then user
can create a module for it, which can be reused in many application that
are requiring it.
Creating Local Module
 File name suppose module_calc.js which consists of following code.
function add(a,b){
return(a+b)
}
module.exports=add
 Create another file calculator.js which consists of following code.
var req=require('./module_cal.js');
result=req(5,4)
console.log("output is"+result);
Export Module
 The module.exports is a special object which is included in every
JavaScript file in the Node.js application by default.
 The module is a variable that represents the current module,
and exports is an object that will be exposed as a module.
 So, whatever assign to module.exports will be exposed as a module.
 Different types as a module using module.exports
 Exports Literals
 Export Object
 Export Function
Export Literals
 Assigning a string literal then it will expose that string literal as a
module.
 Suppose following code is written in mod.js file
module.exports = 'Hello world‘;
 In another app.js file following code is written
var msg = require('./mod.js');
console.log(msg);
Export Object
 Exports is an object. So, can attach properties or methods to it.
 Suppose following code is written in mod.js file
exports.SimpleMessage = 'Hello world';
 In another app.js file following code is written
var msg = require('./mod.js');
console.log(msg.SimpleMessage);
Attach Object to module.exports
 Suppose following code is written in mod.js file
module.exports = {
firstName:‘Bareen',
lastName:‘Shaikh’
}
 In another app.js file following code is written
var msg = require('./mod.js');
console.log(msg.firstName + ' ' + msg.lastName);
Export Function
 Can attach an anonymous function to exports object as shown below
 Suppose following code is written in mod.js file
module.exports = function (msg) {
console.log(msg);
};
 In another app.js file following code is written
var msg = require('./mod.js');
msg('HelloWorld');
Export Function as Class
 In JavaScript, a function can be treated like a class.
 Suppose following code is written in mod.js file
module.exports = function (firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
this.fullName = function () {
return this.firstName + ' ' + this.lastName;
}
}
 In another app.js file following code is written
var req= require('./mod.js');
var person = new req(‘Bareen',‘Shaikh');
console.log(person.fullName());
THANK YOU.

Mais conteúdo relacionado

Semelhante a NodeJs Modules1.pdf

Modular javascript
Modular javascriptModular javascript
Modular javascript
Zain Shaikh
 
Introduction to nodejs
Introduction to nodejsIntroduction to nodejs
Introduction to nodejs
James Carr
 
Nt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language AnalysisNt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language Analysis
Nicole Gomez
 
Learning MVC Part 3 Creating MVC Application with EntityFramework
Learning MVC Part 3 Creating MVC Application with EntityFrameworkLearning MVC Part 3 Creating MVC Application with EntityFramework
Learning MVC Part 3 Creating MVC Application with EntityFramework
Akhil Mittal
 

Semelhante a NodeJs Modules1.pdf (20)

Advanced Node.JS Meetup
Advanced Node.JS MeetupAdvanced Node.JS Meetup
Advanced Node.JS Meetup
 
What's New in Java 9
What's New in Java 9What's New in Java 9
What's New in Java 9
 
Java9
Java9Java9
Java9
 
Java 9 Module System
Java 9 Module SystemJava 9 Module System
Java 9 Module System
 
Modular javascript
Modular javascriptModular javascript
Modular javascript
 
React Basic and Advance || React Basic
React Basic and Advance   || React BasicReact Basic and Advance   || React Basic
React Basic and Advance || React Basic
 
Utility Modules in Node.js.pdf
Utility Modules in Node.js.pdfUtility Modules in Node.js.pdf
Utility Modules in Node.js.pdf
 
Introduction to nodejs
Introduction to nodejsIntroduction to nodejs
Introduction to nodejs
 
IOC + Javascript
IOC + JavascriptIOC + Javascript
IOC + Javascript
 
NodeJS: an Introduction
NodeJS: an IntroductionNodeJS: an Introduction
NodeJS: an Introduction
 
React Native for multi-platform mobile applications
React Native for multi-platform mobile applicationsReact Native for multi-platform mobile applications
React Native for multi-platform mobile applications
 
Warsaw Frontend Meetup #1 - Webpack
Warsaw Frontend Meetup #1 - WebpackWarsaw Frontend Meetup #1 - Webpack
Warsaw Frontend Meetup #1 - Webpack
 
Frankenstein's IDE: NetBeans and OSGi
Frankenstein's IDE: NetBeans and OSGiFrankenstein's IDE: NetBeans and OSGi
Frankenstein's IDE: NetBeans and OSGi
 
Nt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language AnalysisNt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language Analysis
 
Packing for the Web with Webpack
Packing for the Web with WebpackPacking for the Web with Webpack
Packing for the Web with Webpack
 
Social Connections VI — IBM Connections Extensions and Themes Demystified
Social Connections VI — IBM Connections Extensions and Themes DemystifiedSocial Connections VI — IBM Connections Extensions and Themes Demystified
Social Connections VI — IBM Connections Extensions and Themes Demystified
 
Object-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modulesObject-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modules
 
JavaScript Miller Columns
JavaScript Miller ColumnsJavaScript Miller Columns
JavaScript Miller Columns
 
Introduction to Webpack 5.0 Presentation
Introduction to Webpack 5.0 PresentationIntroduction to Webpack 5.0 Presentation
Introduction to Webpack 5.0 Presentation
 
Learning MVC Part 3 Creating MVC Application with EntityFramework
Learning MVC Part 3 Creating MVC Application with EntityFrameworkLearning MVC Part 3 Creating MVC Application with EntityFramework
Learning MVC Part 3 Creating MVC Application with EntityFramework
 

Mais de Bareen Shaikh (11)

Express Generator.pdf
Express Generator.pdfExpress Generator.pdf
Express Generator.pdf
 
Middleware.pdf
Middleware.pdfMiddleware.pdf
Middleware.pdf
 
ExpressJS-Introduction.pdf
ExpressJS-Introduction.pdfExpressJS-Introduction.pdf
ExpressJS-Introduction.pdf
 
Express JS-Routingmethod.pdf
Express JS-Routingmethod.pdfExpress JS-Routingmethod.pdf
Express JS-Routingmethod.pdf
 
FS_module_functions.pptx
FS_module_functions.pptxFS_module_functions.pptx
FS_module_functions.pptx
 
File System.pptx
File System.pptxFile System.pptx
File System.pptx
 
Web Server.pdf
Web Server.pdfWeb Server.pdf
Web Server.pdf
 
NPM.pdf
NPM.pdfNPM.pdf
NPM.pdf
 
NodeJs Modules.pdf
NodeJs Modules.pdfNodeJs Modules.pdf
NodeJs Modules.pdf
 
Introduction to Node JS1.pdf
Introduction to Node JS1.pdfIntroduction to Node JS1.pdf
Introduction to Node JS1.pdf
 
Introduction to Node JS.pdf
Introduction to Node JS.pdfIntroduction to Node JS.pdf
Introduction to Node JS.pdf
 

Último

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Último (20)

Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 

NodeJs Modules1.pdf

  • 1. Prepared By: Prof. Bareen Shaikh Department of Computer Science, MIT ACSC,ALNDI(D), PUNE NodeJs Modules
  • 2. Topics  3.3 Module and ModuleTypes  3.4 Core Module, Local Module  3.5 Directories as module  3.5 Module. exports
  • 3. Module and Module Types  Module in Node.js is a simple or complex functionality organized in single or multiple JavaScript files which can be reused throughout the Node.js application.  Each module in Node.js has its own context, so it cannot interfere with other modules or pollute global scope.  Each module can be placed in a separate .js file under a separate folder.  Node.js ModuleTypes: Node.js includes three types of modules: 1. Core Modules 2. Local Modules
  • 4. Core Module  Node.js is a light weight framework.  The core modules include bare minimum functionalities of Node.js.  These core modules are compiled into its binary distribution and load automatically when Node.js process starts.  Need to be import the core module first in order to use in an application.  Following lists some of the important core modules in Node.js  http  url  util  path  fs
  • 5. Loading Core Module  In order to use Node.js core or NPM modules, need to import it using require() function as shown below. var module = require('module_name');  require() function return an object, function, property or any other JavaScript type, depending on what the specified module returns. Eg. var http = require('http'); var server = http.createServer(function(req, res){ //write code here }); server.listen(5000);
  • 6. Local Module  Local modules are modules created locally by user in Node.js application.  Modules may include different functionalities of application in separate files and folders.  These module can also package it and distribute it via NPM, so that Node.js community can use it.  For example, If an application need to connect to MongoDB and fetch data then user can create a module for it, which can be reused in many application that are requiring it.
  • 7. Creating Local Module  File name suppose module_calc.js which consists of following code. function add(a,b){ return(a+b) } module.exports=add  Create another file calculator.js which consists of following code. var req=require('./module_cal.js'); result=req(5,4) console.log("output is"+result);
  • 8. Export Module  The module.exports is a special object which is included in every JavaScript file in the Node.js application by default.  The module is a variable that represents the current module, and exports is an object that will be exposed as a module.  So, whatever assign to module.exports will be exposed as a module.  Different types as a module using module.exports  Exports Literals  Export Object  Export Function
  • 9. Export Literals  Assigning a string literal then it will expose that string literal as a module.  Suppose following code is written in mod.js file module.exports = 'Hello world‘;  In another app.js file following code is written var msg = require('./mod.js'); console.log(msg);
  • 10. Export Object  Exports is an object. So, can attach properties or methods to it.  Suppose following code is written in mod.js file exports.SimpleMessage = 'Hello world';  In another app.js file following code is written var msg = require('./mod.js'); console.log(msg.SimpleMessage);
  • 11. Attach Object to module.exports  Suppose following code is written in mod.js file module.exports = { firstName:‘Bareen', lastName:‘Shaikh’ }  In another app.js file following code is written var msg = require('./mod.js'); console.log(msg.firstName + ' ' + msg.lastName);
  • 12. Export Function  Can attach an anonymous function to exports object as shown below  Suppose following code is written in mod.js file module.exports = function (msg) { console.log(msg); };  In another app.js file following code is written var msg = require('./mod.js'); msg('HelloWorld');
  • 13. Export Function as Class  In JavaScript, a function can be treated like a class.  Suppose following code is written in mod.js file module.exports = function (firstName, lastName) { this.firstName = firstName; this.lastName = lastName; this.fullName = function () { return this.firstName + ' ' + this.lastName; } }  In another app.js file following code is written var req= require('./mod.js'); var person = new req(‘Bareen',‘Shaikh'); console.log(person.fullName());