UNPKG

nic_training

Version:

Sample demos for training on nodejs

38 lines (31 loc) 1.89 kB
//Modules.js: If a Component has to be used in different apps and different parts of the Application, U could export a module. Module is an independent component that is reusable across differnt apps in UR NodeJs environment. NodeJS itself is made of multiple modules. They are broadly classified as Core Modules, Npm Modules and Custom modules. //Core modules are part of the Nodejs environment that can be used to develop apps. This include fs, binary, http and many more. //Node provides a repository of modules from github that could be downloaded and consumed. We call them as packages. There is a Node Package Manager(npm) which is a component to export and import packages from and to github. The basic idea is to bundle UR Module into a package so that it can be consumed by multiple apps across the globe. Each module is an independent component that can be used my various apps if usable. //Custom modules are ones which we create for our apps and later if required could be exported as an Npm Package and could used later from the repository... //Modules could be created as global variables. U can create a anonymous method which gives a module object. U can also create a named Object which will be used in UR App. module.exports.developedBy ="Phaniraj";//Hold some data... module.exports.developedFor ="NIC, Chennai"; module.exports.Employees = (function(){//Have an object var employees = []; function addNewEmployee(empObj){ employees.push(empObj); return employees.length; } function deleteEmp(id){ for (var i = 0; i < employees.length; i++) { if(employees[i].empID == id){ employees.splice(i, 1); break; } } return employees.length; } function getAllEmployees(){ return employees; } return{ AddNew: addNewEmployee, DeleteEmp : deleteEmp, GetAllEmployees : getAllEmployees } });