Listing 27.7 implements the route handling code for the Photo
model. There are two routes handled. The getPhoto()
route handler looks up a single Photo
document, based on the _id
field passed in as photoId
in the GET
query string. The getPhotos()
route handler retrieves all Photo
documents. Both handlers return a JSON string form of the results.
01 var mongoose = require('mongoose'),
02 Photo = mongoose.model('Photo'),
03 exports.getPhoto = function(req, res) {
04 Photo.findOne({ _id: req.query.photoId })
05 .exec(function(err, photo) {
06 if (!photo){
07 res.json(404, {msg: 'Photo Not Found.'});
08 } else {
09 res.json(photo);
10 }
11 });
12 };
13 exports.getPhotos = function(req, res) {
14 Photo.find()
15 .exec(function(err, photos) {
16 if (!photos){
17 res.json(404, {msg: 'Photos Not Found.'});
18 } else {
19 res.json(photos);
20 }
21 });
22 };