Video Course

Create a CRUD App with Node and MongoDB: Lesson 11 of 17

Refactoring Controllers

Up Next

Showing Real Events

Autoplaying in 7 seconds!

Cancel

It's started to get a little hard to read the events.controller.js file. There are a few functions in there with some code in between and the file is getting larger.

We'll be using what's known as the revealing module pattern to clean up our code a bit. This is where we return an object literal with names that map to the functions we'll need.

For instance, we are using:

// export this object
module.exports = {
  showEvents: showEvents,
  showSingle: showSingle,
  seedEvents: seedEvents
};

function showEvents(req, res)...
function showSingle(req, res)...
function seedEvents(req, res)...

This cleans up our code a bit and lets us easily view what's going on in this file.

Chris Sevilleja

162 posts

Co-founder of Scotch.io. Slapping the keyboard until something good happens.