Create a CRUD App with Node and MongoDB

Refactoring Controllers

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.

Like this article? Follow @chrisoncode on Twitter