June 2014
Intermediate to advanced
696 pages
38h 52m
English
Listing 28.9 implements the route handling code for the Product model. There are only two routes. The getProduct() route finds a single product, based on the productId included in the query. The getProducts() route finds all products. If the requests are successful, the product or all products are returned to the client as JSON strings. If the requests fail, a 404 error is returned.
Listing 28.9 products_controller.js: Implementing routes to get products for the Express server
01 var mongoose = require('mongoose'),02 Product = mongoose.model('Product');03 exports.getProduct = function(req, res) {04 Product.findOne({ _id: req.query.productId })05 .exec(function(err, ...
Read now
Unlock full access