- Explain what a Node.js module is.
- Explain what the three kinds of Node.js modules are.
- Split existing code into two modules.
Turn to your neighbor and discuss what a Node.js module is. What do the module.exports and exports variables do? Be prepared to share with the class.
Turn to your neighbor and identify the three kinds of Node.js modules. Be sure to sepcify how each is imported using the require function.
Turn to your neighbor and discuss the different ways of splitting code up using Node.js modules.
module.exports = function(a, b) {
return a + b;
};module.exports = {
add(a, b) {
return a + b;
}
};module.exports.add = function(a, b) {
return a + b;
};function add(a, b) {
return a + b;
};
module.exports = {
add
}Lets put these modules to work and practice refacotring code in an express app.
Start by cloning this repo:
git clone https://github.com/Shurlow/modules-lesson.git
Our goal is to refactor the fs code in server.js into a custom module
- Create a function called
getGuestsin theguestsSync.js- use synchronous
fs.readFileSyncto readguests.json. - update
server.jsto use this module and function
- use synchronous
- Create a function called
getGuestByIDin theguestsSync.js - Create a function called
addGuestin theguestsSync.js
- Create a function called
getGuestsin theguestsAsync.js- use asynchronous
fs.readFileto readguests.json. - pass a callback function as the parameter to this function
- use asynchronous
- Create a function called
getGuestByIDin theguestsAsync.js - Create a function called
addGuestin theguestsAsync.js