Created
June 27, 2019 16:57
-
-
Save BretCameron/b150f9ba9fa8432dd0aab39d42b95a77 to your computer and use it in GitHub Desktop.
A simple server with CRUD enpoints built using Node.js, Express, MongoDB and Mongoose
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const express = require('express'); | |
| const mongoose = require('mongoose'); | |
| const config = require('config'); | |
| const app = express(); | |
| app.use(express.json()); | |
| const db = config.get('mongoURI'); | |
| const Animal = require('./models/Animal'); | |
| mongoose | |
| .connect(db, { useNewUrlParser: true, useCreateIndex: true, useFindAndModify: false }) | |
| .then(() => console.log('MongoDB Connected...')) | |
| .catch(err => console.log(err)); | |
| // Read all entries | |
| app.get('/', (req, res) => { | |
| Animal.find() | |
| .sort({ date: -1 }) | |
| .then(items => console.log(res.json(items))); | |
| }); | |
| // Add a new entry | |
| app.post('/', (req, res) => { | |
| const newAnimal = new Animal({ | |
| name: req.body.name, | |
| isEndangered: req.body.isEndangered || false, | |
| }); | |
| newAnimal | |
| .save() | |
| .then(item => res.json(item)); | |
| }); | |
| // Delete an entry | |
| app.delete('/:id', (req, res) => { | |
| Animal.findOneAndDelete({ _id: req.params.id }) | |
| .then(() => res.json({ success: true })) | |
| .catch(err => res.status(404).json({ success: false })); | |
| }); | |
| // Update an entry | |
| app.put('/:id', (req, res) => { | |
| Animal.findOneAndUpdate({ _id: req.params.id }, req.body) | |
| .then(() => res.json({ success: true })) | |
| .catch(err => res.status(404).json({ success: false })); | |
| }); | |
| const port = 5000; | |
| app.listen(port, () => console.log(`Server started on port: http://localhost:${port}`)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment