Skip to content

Instantly share code, notes, and snippets.

@Shurlow
Last active July 26, 2017 23:17
Show Gist options
  • Select an option

  • Save Shurlow/1e7c35160d65bd2e33055aee862d20bf to your computer and use it in GitHub Desktop.

Select an option

Save Shurlow/1e7c35160d65bd2e33055aee862d20bf to your computer and use it in GitHub Desktop.
Node Modules Instructor Notes

Node Modules Instructor Notes

Objectives

  • Explain what a Node.js module is.
  • Explain what the three kinds of Node.js modules are.
  • Split existing code into two modules.

Explain what a Node.js module is

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.

Explain what the three kinds of Node.js modules are

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.

Split existing code into two modules

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
}

Refactoring Express

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

Simply Synchronous

  • Create a function called getGuests in the guestsSync.js
    • use synchronous fs.readFileSync to read guests.json.
    • update server.js to use this module and function
  • Create a function called getGuestByID in the guestsSync.js
  • Create a function called addGuest in the guestsSync.js

Absolutely Async

  • Create a function called getGuests in the guestsAsync.js
    • use asynchronous fs.readFile to read guests.json.
    • pass a callback function as the parameter to this function
  • Create a function called getGuestByID in the guestsAsync.js
  • Create a function called addGuest in the guestsAsync.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment