Skip to content

Instantly share code, notes, and snippets.

@jaouadballat
Created March 3, 2018 16:51
Show Gist options
  • Save jaouadballat/560f1abf131a8eb1fe3838662bf76148 to your computer and use it in GitHub Desktop.
Save jaouadballat/560f1abf131a8eb1fe3838662bf76148 to your computer and use it in GitHub Desktop.
const express = require('express');
const router = express.Router();
const Category = require('../models/Category');
const Product = require('../models/Product');
router.get('/', function (req, res, next) {
Category.find(function (err, categories) {
if (err) return console.log(err);
res.status(200).json(categories);
});
});
//display all products in a specific Category
router.get('/:category', function (req, res, next) {
Category.findOne({title: req.params.category}, function (err, category) {
if (err) return console.log(err);
Product.find({category: category.title}, function(err, products) {
if(err) return console.log(err);
res.status(200).json(products);
});
});
});
module.exports = router;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment