Book Image

Web Development with MongoDB and Node.js

By : Jason Krol
Book Image

Web Development with MongoDB and Node.js

By: Jason Krol

Overview of this book

This book is designed for developers of any skill level that want to get up and running using Node.js and MongoDB to build full featured web applications. A basic understanding of JavaScript and HTML is the only requirement for this book.
Table of Contents (14 chapters)
12
12. Popular Node.js Web Frameworks
13
Index

Routers and controllers


So far, you have your server.js file and a configure module that is used to wire up all of the necessary middleware for the application. The next step is to implement a proper router and controller.

The router is going to be a map of each of the available URL paths for the app. Every route on the server will correspond to a function in a controller. Here is what our routes table will look like for the particular application we are writing:

GET  /(index) - home.index (render the homepage of the site)
GET  /images/image_id -
 image.index (render the page for a specific image)
POST /images - image.create (when a user submits and uploads a new image)
POST /images/image_id/like - image.like (when a user clicks the Like button)
POST /images/image_id/comment - image.comment (when a user posts a comment)

You can see that we are handling two different GET requests and three different POST requests. In addition, we have two main controllers: home and image. Controllers are really...