-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
44 lines (32 loc) · 994 Bytes
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
require('dotenv').config()
const express = require('express')
const mongoose = require('mongoose')
const cors = require('cors')
const app = express()
const Recipes = require('./models/recipeschema')
const mongoURI = process.env.MONGODB_URI
app.use(express.json())
app.use(cors())
app.get('/recipes', async (req, res) => {
const allRecipes = await Recipes.find({})
res.json(allRecipes)
})
app.post('/recipes', async (req, res) => {
const newRecipe = await Recipes.create(req.body)
res.json(newRecipe)
})
app.put('/recipes/:id', async (req, res) => {
const updatedRecipe = await Recipes.findByIdAndUpdate(req.params.id, req.body, {new:true})
res.json(updatedRecipe)
})
app.delete('/recipes/:id', async (req, res) => {
const deleteRecipe = await Recipes.findByIdAndRemove(req.params.id)
res.json(deleteRecipe)
})
app.listen(3000, () => {
console.log('listening...')
})
mongoose.connect(mongoURI)
.then(()=>{
console.log("Connected to Atlas")
})