-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
68 lines (59 loc) · 1.52 KB
/
app.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// express app
const express = require('express')
const cors = require('cors')
const bodyParser = require('body-parser')
const mongoose = require('mongoose')
const Todo = require('./models/Todo')
const User = require('./models/User')
const app = express()
// connect to mongodb atlas
const dbURI = 'mongodb+srv://amandacarv:[email protected]/todo-calendar?retryWrites=true&w=majority'
mongoose.connect(dbURI, {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(res => app.listen(3001))
.catch(err => console.log(err))
;
// middleware
app.use(bodyParser.urlencoded( {extended: true} ))
app.use(cors())
app.use(bodyParser.json())
app.post('/todos/new', (req, res) => {
const todo = new Todo({
title: req.body.title,
description: req.body.description,
time: req.body.time
})
todo.save()
.then((todo) => {
res.send(todo)
})
.catch((err) => {
console.log(err)
})
})
// read todo
app.get('/todos', (req, res) => {
Todo.find()
.then((todos) => {
res.send(todos)
})
.catch((err) => {
console.log(err)
})
})
// delete todo
app.delete('/todos/:id', (req, res) => {
Todo.findById()
todoList.splice(todoid, 1);
res.json();
})
// 'update' todo (set as done)
app.put('/todos/done/:id', (req, res) => {
const todoid = req.params.id;
const todo = todoList[todoid];
doneList.push(todo);
todoList.splice(todoid, 1);
res.json();
})