├── app.js ├── controllers └── product.js ├── models └── product.js ├── package.json └── routes └── product.js /app.js: -------------------------------------------------------------------------------- 1 | // app.js 2 | 3 | var express = require('express'); 4 | var bodyParser = require('body-parser'); 5 | 6 | var product = require('./routes/product'); // Imports routes for the products 7 | var app = express(); 8 | 9 | 10 | // Set up mongoose connection 11 | var mongoose = require('mongoose'); 12 | var dev_db_url = 'mongodb://someuser:abcd1234@ds123619.mlab.com:23619/productstutorial'; 13 | var mongoDB = process.env.MONGODB_URI || dev_db_url; 14 | mongoose.connect(mongoDB); 15 | mongoose.Promise = global.Promise; 16 | var db = mongoose.connection; 17 | db.on('error', console.error.bind(console, 'MongoDB connection error:')); 18 | 19 | app.use(bodyParser.json()); 20 | app.use(bodyParser.urlencoded({extended: false})); 21 | app.use('/products', product); 22 | 23 | var port = 1234; 24 | 25 | app.listen(port, () => { 26 | console.log('Server is up and running on port numner ' + port); 27 | }); 28 | -------------------------------------------------------------------------------- /controllers/product.js: -------------------------------------------------------------------------------- 1 | var Product = require('../models/product'); 2 | 3 | //Simple version, without validation or sanitation 4 | exports.test = function (req, res) { 5 | res.send('Greetings from the Test controller!'); 6 | }; 7 | 8 | exports.product_create = function (req, res) { 9 | var product = new Product( 10 | { 11 | name: req.body.name, 12 | price: req.body.price 13 | } 14 | ); 15 | 16 | product.save(function (err) { 17 | if (err) { 18 | return next(err); 19 | } 20 | res.send('Product Created successfully') 21 | }) 22 | }; 23 | 24 | exports.product_details = function (req, res) { 25 | Product.findById(req.params.id, function (err, product) { 26 | if (err) return next(err); 27 | res.send(product); 28 | }) 29 | }; 30 | 31 | exports.product_update = function (req, res) { 32 | Product.findByIdAndUpdate(req.params.id, {$set: req.body}, function (err, product) { 33 | if (err) return next(err); 34 | res.send('Product udpated.'); 35 | }); 36 | }; 37 | 38 | exports.product_delete = function (req, res) { 39 | Product.findByIdAndRemove(req.params.id, function (err) { 40 | if (err) return next(err); 41 | res.send('Deleted successfully!'); 42 | }) 43 | }; -------------------------------------------------------------------------------- /models/product.js: -------------------------------------------------------------------------------- 1 | var mongoose = require('mongoose'); 2 | var Schema = mongoose.Schema; 3 | 4 | var ProductSchema = new Schema({ 5 | name: {type: String, required: true, max: 100}, 6 | price: {type: Number, required: true}, 7 | }); 8 | 9 | 10 | // Export the model 11 | module.exports = mongoose.model('Product', ProductSchema); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "productsapp", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "body-parser": "^1.18.2", 13 | "express": "^4.16.3", 14 | "mongoose": "^5.0.11" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /routes/product.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var router = express.Router(); 3 | 4 | // Require the controllers WHICH WE DID NOT CREATE YET!! 5 | var product_controller = require('../controllers/product'); 6 | 7 | 8 | // a simple test url to check that all of our files are communicating correctly. 9 | router.get('/test', product_controller.test); 10 | 11 | 12 | router.post('/create', product_controller.product_create); 13 | 14 | router.get('/:id', product_controller.product_details); 15 | 16 | router.put('/:id/update', product_controller.product_update); 17 | 18 | router.delete('/:id/delete', product_controller.product_delete); 19 | 20 | 21 | module.exports = router; --------------------------------------------------------------------------------