├── .gitignore ├── bin └── dev ├── .env ├── .babelrc ├── models └── bookModel.js ├── readme.md ├── server.js ├── package.json └── Routes └── bookRouter.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | mynotes -------------------------------------------------------------------------------- /bin/dev: -------------------------------------------------------------------------------- 1 | require('dotenv/config') 2 | require('./../server.js') -------------------------------------------------------------------------------- /.env: -------------------------------------------------------------------------------- 1 | DB_ADDRESS = mongodb://:@ds125068.mlab.com:25068/ -------------------------------------------------------------------------------- /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["es2015-node5", "stage-3"], 3 | "plugins": [] 4 | } -------------------------------------------------------------------------------- /models/bookModel.js: -------------------------------------------------------------------------------- 1 | import mongoose from 'mongoose'; 2 | 3 | const Schema = mongoose.Schema; 4 | 5 | const bookModel = new Schema({ 6 | title: { type: String }, 7 | author: { type: String } 8 | }); 9 | export default mongoose.model('books', bookModel) -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | ## Building a RESTful API with Express and MongoDB 2 | 3 | This project is the product of a tutorial I wrote with the same title, you can [read it here](https://dev.to/aurelkurtula/building-a-restful-api-with-express-and-mongodb--3mmh). The API interacts with a mongoDB through get, post, put, patch and delete. 4 | 5 | ### Instructions 6 | 7 | Install requered packages 8 | 9 | npm install 10 | 11 | Change the database adress in `.env` 12 | 13 | Run server 14 | 15 | npm start 16 | 17 | Visit `http://localhost:5656/api/books/` for the API root. -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | import express from 'express'; 2 | import mongoose from 'mongoose'; 3 | import bodyParser from 'body-parser'; 4 | import bookRouter from './Routes/bookRouter'; 5 | const app = express(); 6 | const port = process.env.PORT || 5656; 7 | // Connecting to the database 8 | const db = mongoose.connect(process.env.DB_ADDRESS); 9 | 10 | // setting body parser middleware 11 | app.use(bodyParser.json()); 12 | app.use(bodyParser.urlencoded({ extended: true })); 13 | 14 | // API routes 15 | app.use('/api/Books', bookRouter); 16 | 17 | // Running the server 18 | app.listen(port, () => { 19 | console.log(`http://localhost:${port}`) 20 | }) 21 | 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "quick", 3 | "version": "1.0.0", 4 | "description": ":) ", 5 | "scripts": { 6 | "start": "nodemon --exec babel-node server.js" 7 | }, 8 | "repository": { 9 | "type": "git", 10 | "url": "" 11 | }, 12 | "author": "Aurel kurtula", 13 | "license": "MIT", 14 | "homepage": "", 15 | "dependencies": { 16 | "body-parser": "^1.18.2", 17 | "dotenv": "^5.0.0", 18 | "express": "^4.16.2", 19 | "mongoose": "^5.0.1" 20 | }, 21 | "devDependencies": { 22 | "babel-preset-stage-3": "^6.22.0", 23 | "babel-cli": "*", 24 | "babel-core": "*", 25 | "babel-preset-es2015-node5": "*", 26 | "babel-register": "*" 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Routes/bookRouter.js: -------------------------------------------------------------------------------- 1 | import express from 'express'; 2 | import Book from '../models/bookModel'; 3 | const bookRouter = express.Router(); 4 | bookRouter.route('/') 5 | .get((req, res) => { 6 | Book.find({}, (err, books) => { 7 | res.json(books) 8 | }) 9 | }) 10 | .post((req, res) => { 11 | let book = new Book(req.body); 12 | book.save(); 13 | res.status(201).send(book) 14 | }) 15 | 16 | // Middleware 17 | bookRouter.use('/:bookId', (req, res, next)=>{ 18 | Book.findById( req.params.bookId, (err,book)=>{ 19 | if(err) 20 | res.status(500).send(err) 21 | else { 22 | req.book = book; 23 | next() 24 | } 25 | }) 26 | 27 | }) 28 | bookRouter.route('/:bookId') 29 | .get((req, res) => { 30 | res.json(req.book) 31 | }) // end get Books/:bookId 32 | .put((req,res) => { 33 | req.book.title = req.body.title; 34 | req.book.author = req.body.author; 35 | req.book.save() 36 | res.json(req.book) 37 | }) 38 | .patch((req,res)=>{ 39 | if(req.body._id){ 40 | delete req.body._id; 41 | } 42 | for( let p in req.body ){ 43 | req.book[p] = req.body[p] 44 | } 45 | req.book.save() 46 | res.json(req.book) 47 | })//patch 48 | .delete((req,res)=>{ 49 | req.book.remove(err => { 50 | if(err){ 51 | res.status(500).send(err) 52 | } 53 | else{ 54 | res.status(204).send('removed') 55 | } 56 | }) 57 | })//delete 58 | 59 | export default bookRouter; --------------------------------------------------------------------------------