Advertisement
Guest User

Untitled

a guest
Jan 5th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require('express');
  2. const router = express.Router();
  3. const mongoose =  require('mongoose');
  4. const Product = require('../models/product');
  5.  
  6.  
  7. router.get('/', (req, res, next) => {
  8.     res.status(200).json({
  9.         message: 'Handling GET requests to /products'
  10.     });
  11. });
  12.  
  13. router.post('/', (req,res,next)=>{
  14.     const product = new Product({
  15.         _id: new mongoose.Types.ObjectId(),
  16.         name: req.body.name,
  17.         price: req.body.price
  18.     });
  19.     product
  20.     .save()
  21.     .then(result =>{
  22.         console.log(result);
  23.     })
  24.     .catch(err => console.log(err));
  25.     res.status(201).json({
  26.         message: 'Handling POST requests to /products',
  27.         createdProduct: product
  28.     });
  29. });
  30.  
  31. router.get('/:productId', (req,res,next)=>{
  32.     const id = req.params.productId;
  33.     if(id === 'special'){
  34.         res.status(200).json({
  35.             message:'You discovered the special ID',
  36.             id: id
  37.         });
  38.     } else {
  39.         res.status(200).json({
  40.             message: 'You passed and ID'
  41.         });
  42.     }
  43. });
  44.  
  45. router.patch('/:productId', (req,res,next)=>{
  46.     res.status(200).json({
  47.         message:'Udated product!'
  48.     });
  49. });
  50.  
  51. router.delete('/:productId', (req,res,next)=>{
  52.     res.status(200).json({
  53.         message:'Deleted product!'
  54.     });
  55. });
  56.  
  57.  
  58. module.exports=router;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement