├── package.json ├── product.js └── server.js /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "product-api", 3 | "main": "server.js", 4 | "dependencies": { 5 | "express": "~4.0.0", 6 | "body-parser": "~1.0.1", 7 | "cors": "2.8.1", 8 | "mongoose": "~3.6.13" 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /product.js: -------------------------------------------------------------------------------- 1 | var mongoose = require('mongoose'); 2 | var Schema = mongoose.Schema; 3 | var ProductSchema = new Schema({ 4 | title: String, 5 | price: Number, 6 | instock : Boolean, 7 | photo : String , 8 | }); 9 | module.exports = mongoose.model('Product', ProductSchema); 10 | // module.exports = mongoose.model('Product', ProductSchema,'optiponally pass schema name '); 11 | 12 | -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var bodyParser = require('body-parser'); 3 | var cors = require('cors'); 4 | var app = express(); 5 | var mongoose = require('mongoose'); 6 | var product = require('./product'); 7 | 8 | app.use(bodyParser.urlencoded({ extended: true })); 9 | app.use(bodyParser.json()); 10 | var port = process.env.PORT || 8090; 11 | var router = express.Router(); 12 | 13 | mongoose.connect('mongodb://localhost:27017/products'); 14 | 15 | // Middle Route 16 | 17 | router.use(function (req, res, next) { 18 | // do logging 19 | // do authentication 20 | console.log('Logging of request will be done here'); 21 | next(); // make sure we go to the next routes and don't stop here 22 | }); 23 | 24 | 25 | router.route('/products').post(function (req, res) { 26 | console.log("in add"); 27 | var p = new product(); 28 | p.title = req.body.title; 29 | p.price = req.body.price; 30 | p.instock = req.body.instock; 31 | p.photo = req.body.photo; 32 | p.save(function (err) { 33 | if (err) { 34 | res.send(err); 35 | } 36 | console.log("added"); 37 | res.send({ message: 'Product Created !' }) 38 | }) 39 | }); 40 | 41 | router.route('/products').get(function (req, res) { 42 | product.find(function (err, products) { 43 | if (err) { 44 | res.send(err); 45 | } 46 | res.send(products); 47 | }); 48 | }); 49 | 50 | router.route('/products/:product_id').get(function (req, res) { 51 | 52 | 53 | product.findById(req.params.product_id, function (err, prod) { 54 | if (err) 55 | res.send(err); 56 | res.json(prod); 57 | }); 58 | }); 59 | 60 | router.route('/products/:product_id').put(function (req, res) { 61 | 62 | product.findById(req.params.product_id, function (err, prod) { 63 | if (err) { 64 | res.send(err); 65 | } 66 | prod.title = req.body.title; 67 | prod.price = req.body.price; 68 | prod.instock = req.body.instock; 69 | prod.photo = req.body.photo; 70 | prod.save(function (err) { 71 | if (err) 72 | res.send(err); 73 | 74 | res.json({ message: 'Product updated!' }); 75 | }); 76 | 77 | }); 78 | }); 79 | 80 | router.route('/products/:product_id').delete(function (req, res) { 81 | 82 | product.remove({ _id: req.param.product_id }, function (err, prod) { 83 | if (err) { 84 | res.send(err); 85 | } 86 | res.json({ message: 'Successfully deleted' }); 87 | }) 88 | 89 | }); 90 | 91 | 92 | app.use(cors()); 93 | app.use('/api', router); 94 | app.listen(port); 95 | console.log('REST API is runnning at ' + port); 96 | --------------------------------------------------------------------------------