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.
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 };
52.15.65.65