Anúncio

NodeJs Modules1.pdf

Education Employee em MIT
30 de Jan de 2023
Anúncio

Mais conteúdo relacionado

Anúncio

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());
  14. THANK YOU.
Anúncio