├── .gitignore ├── README.md ├── app.js ├── models └── item.js ├── package.json ├── routers └── item.js └── update.sh /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.log -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RESTful API with Node.js, Express.js, Mongoose and MongoDB. 2 | 3 | Example of a RESTful API built with Node.js, Express.js, Mongoose and MongoDB. 4 | 5 | ## RESTful API endpoints 6 | 7 | ### GET `/api/items` 8 | 9 | Get all items. 10 | 11 | + Method: `GET` 12 | + URL: `/api/items` 13 | 14 | ### POST `/api/items` 15 | 16 | Create a new item. 17 | 18 | + Method: `POST` 19 | + URL: `/api/items` 20 | + Body: 21 | 22 | ```js 23 | { 24 | "id": "1", 25 | "name": "React.js Essentials", 26 | "description": "A fast-paced guide to designing and building scalable and maintainable web apps with React.js.", 27 | "quantity": "10" 28 | } 29 | ``` 30 | 31 | ### GET `/api/items/:itemId` 32 | 33 | Get item with specific id. 34 | 35 | + Method: `GET` 36 | + URL: `/api/items/1` 37 | 38 | ### PUT `/api/items/:itemId` 39 | 40 | Update entire item with specific id. 41 | 42 | + Method: `PUT` 43 | + URL: `/api/items/1` 44 | + Body: 45 | 46 | ```js 47 | { 48 | "id": "1", 49 | "name": "React.js Essentials", 50 | "description": "A fast-paced guide to designing and building scalable and maintainable web apps with React.js.", 51 | "quantity": "20" 52 | } 53 | ``` 54 | 55 | ### PATCH `/api/items/:itemId` 56 | 57 | Update part of the item with specific id. 58 | 59 | + Method: `PATCH` 60 | + URL: `/api/items/1` 61 | + Body: 62 | 63 | ```js 64 | { 65 | "quantity": "30" 66 | } 67 | ``` 68 | 69 | ### DELETE `/api/items/:itemId` 70 | 71 | Delete item with specific id. 72 | 73 | + Method: `DELETE` 74 | + URL: `/api/items/1` 75 | 76 | ## Install 77 | 78 | `npm install` 79 | 80 | ## Run 81 | 82 | 0. Make sure MongoDB is running, if not: `sudo ~/mongodb/bin/mongod` (assuming you have `~/mongodb` directory). 83 | 1. `npm run start` 84 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var mongoose = require('mongoose'); 3 | var bodyParser = require('body-parser'); 4 | var itemRouter = require('./routers/item'); 5 | 6 | var app = express(); 7 | 8 | var PORT = 8080; 9 | var HOST_NAME = 'localhost'; 10 | var DATABASE_NAME = 'shoppingList'; 11 | 12 | mongoose.connect('mongodb://' + HOST_NAME + '/' + DATABASE_NAME); 13 | 14 | app.use(bodyParser.json()); 15 | app.use(bodyParser.urlencoded({ 16 | extended: true 17 | })); 18 | 19 | app.use('/api', itemRouter); 20 | 21 | app.listen(PORT, function () { 22 | console.log('Listening on port ' + PORT); 23 | }); 24 | -------------------------------------------------------------------------------- /models/item.js: -------------------------------------------------------------------------------- 1 | var mongoose = require('mongoose'); 2 | var Schema = mongoose.Schema; 3 | 4 | var itemSchema = new Schema({ 5 | id: { 6 | type: Number, 7 | unique: true, 8 | required: true 9 | }, 10 | name: { 11 | type: String, 12 | required: true 13 | }, 14 | description: { 15 | type: String, 16 | required: true 17 | }, 18 | quantity: { 19 | type: Number, 20 | required: true, 21 | default: 1 22 | } 23 | }, { collection: 'dataItems' }); 24 | 25 | module.exports = mongoose.model('Item', itemSchema); 26 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "restful-api-express-mongoose", 3 | "version": "1.0.0", 4 | "description": "Example of a RESTful API built with Node.js, Express.js and Mongoose.", 5 | "main": "app.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "start": "node app" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "https://github.com/fedosejev/restful-api-express-mongoose.git" 13 | }, 14 | "author": "Artemij Fedosejev", 15 | "license": "ISC", 16 | "bugs": { 17 | "url": "https://github.com/fedosejev/restful-api-express-mongoose/issues" 18 | }, 19 | "homepage": "https://github.com/fedosejev/restful-api-express-mongoose", 20 | "dependencies": { 21 | "body-parser": "^1.14.1", 22 | "express": "^4.13.3", 23 | "mongoose": "^4.2.9" 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /routers/item.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var Item = require('../models/item'); 3 | 4 | var itemRouter = express.Router(); 5 | 6 | itemRouter 7 | .route('/items') 8 | .post(function (request, response) { 9 | 10 | console.log('POST /items'); 11 | 12 | var item = new Item(request.body); 13 | 14 | item.save(); 15 | 16 | response.status(201).send(item); 17 | }) 18 | .get(function (request, response) { 19 | 20 | console.log('GET /items'); 21 | 22 | Item.find(function (error, items) { 23 | 24 | if (error) { 25 | response.status(500).send(error); 26 | return; 27 | } 28 | 29 | console.log(items); 30 | 31 | response.json(items); 32 | }); 33 | }); 34 | 35 | itemRouter 36 | .route('/items/:itemId') 37 | .get(function (request, response) { 38 | 39 | console.log('GET /items/:itemId'); 40 | 41 | var itemId = request.params.itemId; 42 | 43 | Item.findOne({ id: itemId }, function (error, item) { 44 | 45 | if (error) { 46 | response.status(500).send(error); 47 | return; 48 | } 49 | 50 | console.log(item); 51 | 52 | response.json(item); 53 | 54 | }); 55 | }) 56 | .put(function (request, response) { 57 | 58 | console.log('PUT /items/:itemId'); 59 | 60 | var itemId = request.params.itemId; 61 | 62 | Item.findOne({ id: itemId }, function (error, item) { 63 | 64 | if (error) { 65 | response.status(500).send(error); 66 | return; 67 | } 68 | 69 | if (item) { 70 | item.name = request.body.name; 71 | item.description = request.body.description; 72 | item.quantity = request.body.quantity; 73 | 74 | item.save(); 75 | 76 | response.json(item); 77 | return; 78 | } 79 | 80 | response.status(404).json({ 81 | message: 'Item with id ' + itemId + ' was not found.' 82 | }); 83 | }); 84 | }) 85 | .patch(function (request, response) { 86 | 87 | console.log('PATCH /items/:itemId'); 88 | 89 | var itemId = request.params.itemId; 90 | 91 | Item.findOne({ id: itemId }, function (error, item) { 92 | 93 | if (error) { 94 | response.status(500).send(error); 95 | return; 96 | } 97 | 98 | if (item) { 99 | 100 | for (var property in request.body) { 101 | if (request.body.hasOwnProperty(property)) { 102 | if (typeof item[property] !== 'undefined') { 103 | item[property] = request.body[property]; 104 | } 105 | } 106 | } 107 | 108 | // if (request.body.name) { 109 | // item.name = request.body.name; 110 | // } 111 | 112 | // if (request.body.description) { 113 | // item.description = request.body.description; 114 | // } 115 | 116 | // if (request.body.quantity) { 117 | // item.quantity = request.body.quantity; 118 | // } 119 | 120 | item.save(); 121 | 122 | response.json(item); 123 | return; 124 | } 125 | 126 | response.status(404).json({ 127 | message: 'Item with id ' + itemId + ' was not found.' 128 | }); 129 | }); 130 | }) 131 | .delete(function (request, response) { 132 | 133 | console.log('DELETE /items/:itemId'); 134 | 135 | var itemId = request.params.itemId; 136 | 137 | Item.findOne({ id: itemId }, function (error, item) { 138 | 139 | if (error) { 140 | response.status(500).send(error); 141 | return; 142 | } 143 | 144 | if (item) { 145 | item.remove(function (error) { 146 | 147 | if (error) { 148 | response.status(500).send(error); 149 | return; 150 | } 151 | 152 | response.status(200).json({ 153 | 'message': 'Item with id ' + itemId + ' was removed.' 154 | }); 155 | }); 156 | } else { 157 | response.status(404).json({ 158 | message: 'Item with id ' + itemId + ' was not found.' 159 | }); 160 | } 161 | }); 162 | }); 163 | 164 | module.exports = itemRouter; -------------------------------------------------------------------------------- /update.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | git add . 4 | git commit -m "Update" 5 | git push 6 | --------------------------------------------------------------------------------