Creating the controllers folder
Instead of using the routes
folder to create the route and functions of the comments file, we will use another format and create the controllers
folder, where we can separate the route and the controller function, thus having a better modularization:
- Create a folder called
controllers
. - Create a file called
comments.js
and add the following code:// get gravatar icon from email var gravatar = require('gravatar'); // get Comments model var Comments = require('../models/comments'); // List Comments exports.list = function(req, res) { // List all comments and sort by Date Comments.find().sort('-created').populate('user', 'local.email').exec(function(error, comments) { ...
Get Node.js 6.x Blueprints now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.