Book Image

Web Development with MongoDB and NodeJS Second edition

Book Image

Web Development with MongoDB and NodeJS Second edition

Overview of this book

Table of Contents (19 chapters)
Web Development with MongoDB and NodeJS Second Edition
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
12
Popular Node.js Web Frameworks
Index

Controllers


A controller can be defined as the entity that will be responsible for manipulating models and initiating the view render process with the data received from the corresponding models. In the code we have developed so far, we can see that the express router instance is used to tie functions to a corresponding route. These functions are nothing but controllers. For every route that we create in our router, two parameters are necessary:

  • The first parameter is the string for the route itself, which is /images/:image_id

  • The second parameter is a controller function that will be executed when that route is accessed

For any route that has to do with images, we rely on the images controller. Likewise, any route that has to do with the home page relies on the home controller, and so on and so forth.

The steps we will take to define our controllers in our app are purely organizational and based on personal preference. We created our controllers as modules so that our router wasn't a big,...