Express doesn't enforce an opinion on how you should structure the Model, View, and Controller modules of your application, or whether you should follow any kind of MVC paradigm at all. As we learned in the previous chapter, the blank application created by the Express Generator provides two aspects of the MVC model:
- The views directory contains template files, controlling the display portion, corresponding to the View.
- The routes directory contains code implementing the URLs recognized by the application and coordinating the response to each URL. This corresponds to the controller.
This leaves you wondering where to put code corresponding to the model. Models hold the application data, changing it as instructed by the controller and supplying data requested by View code. At a minimum, the Model code should be in separate modules from the Controller code. This is to ensure a clean separation of concerns, for example, to ease the unit testing of each.
The approach we'll use is to create a models directory as a sibling of the views and routes directories. The models directory will hold modules for storing the notes and related data. The API of the modules in the models directory will provide functions to create, read, update, or delete data items Create, Read, Update, and Delete or Destroy (CRUD model) and other functions necessary for the View code to do its thing.
The CRUD model (create, read, update, destroy) is the four basic operations of persistent data storage. The Notes application is structured as a CRUD application to demonstrate implementing each of these operations.
We'll use functions named create, read, update, and destroy to implement each of the basic operations.