-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (61 loc) · 1.45 KB
/
index.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
69
70
71
72
require("dotenv").config()
const mongoose = require('mongoose')
const express = require ('express')
const app = express()
const bodyParser = require ('body-parser')
let jsonParser = bodyParser.json()
var cors = require('cors')
mongoose.connect(process.env.DB_URL, { useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: true } )
const todoSchema = {content: String, done: Boolean}
const Todo = mongoose.model('Todo',todoSchema)
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(cors())
app.get('/todo',function(req,res){
Todo.find(function(err,todos){
if(err){
console.log(err)
}
else{
res.send(todos)
}
})
})
app.post('/todo',jsonParser,function(req,res){
const todo = new Todo({content: req.body.content, done: false})
todo.save(function(err){
if(err){
console.log(err)
}
else{
res.send('entrada adicionada')
}
})
})
app.delete('/todo/:id',jsonParser,function(req,res){
Todo.deleteOne({_id: req.params.id},function(err){
if(err){
console.log(err)
}
else{
res.send('entrada deletada')
}
})
})
app.put('/todo/:id',jsonParser,function(req,res){
Todo.updateOne({_id: req.params.id},{done: req.body.done},function(err){
if(err){
console.log(err)
}
else{
res.send('entrada alterada')
}
})
})
app.get('/',function(req,res){
res.send('hello world')
})
app.listen(3001,function(req,res){
console.log('app started on port 3001')
})