Implementing the Page Model Controller

Listing 27.6 implements the route handling code for the Page model. There is only one route, the getPage() route, which finds the page based on the name field and returns an error or the JSON string form of the object. The name of the page to find comes in as pageName in the GET query string.

Listing 27.6 pages_controller.js: Implementing the getPage route for the Express server


01 var mongoose = require('mongoose'),
02     Page = mongoose.model('Page'),
03 exports.getPage = function(req, res) {
04   Page.findOne({ name: req.query.pageName })
05   .exec(function(err, page) {
06     if (!page){
07       res.json(404, {msg: 'Page Not Found.'});
08     } else {
09       res.json(page);
10     }
11   });
12 };


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.110.106