forked from sf-wdi-25/test-driven-todo-api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
132 lines (111 loc) · 3.08 KB
/
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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// require express and other modules
const express = require('express'),
app = express(),
bodyParser = require('body-parser'),
mongoose = require('mongoose');
cors = require('cors');
var corsOptions = {
origin: 'http://localhost:3000',
credentials: true
}
app.use(cors(corsOptions))
// configure bodyParser (for receiving form data)
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// serve static files from public folder
app.use(express.static(__dirname + '/public'));
mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost/todo');
// require Todo model
var Todo = require('./models/todo');
// get all todos
app.get('/api/todos', function (req, res) {
// find all todos in db
Todo.find(function (err, allTodos) {
if (err) {
res.status(500).json({ error: err.message });
} else {
res.json({ todos: allTodos });
}
});
});
// create new todo
app.post('/api/todos', function (req, res) {
console.log(req.body)
// create new todo with form data (`req.body`)
var newTodo = new Todo(req.body);
console.log(req.body)
// save new todo in db
newTodo.save(function (err, savedTodo) {
if (err) {
res.status(500).json({ error: err.message });
} else {
res.json(savedTodo);
}
});
});
// get one todo
app.get('/api/todos/:id', function (req, res) {
// get todo id from url params (`req.params`)
var todoId = req.params.id;
// find todo in db by id
Todo.findOne({ _id: todoId }, function (err, foundTodo) {
if (err) {
if (err.name === "CastError") {
res.status(404).json({ error: "Nothing found by this ID." });
} else {
res.status(500).json({ error: err.message });
}
} else {
res.json(foundTodo);
}
});
});
// update todo
app.put('/api/todos/:id', function (req, res) {
// get todo id from url params (`req.params`)
var todoId = req.params.id;
console.log(req.body)
// find todo in db by id
Todo.findOne({ _id: todoId }, function (err, foundTodo) {
if (err) {
res.status(500).json({ error: err.message });
} else {
// update the todos's attributes
if(req.body.task) {
foundTodo.task = req.body.task;
} if(req.body.status) {
foundTodo.status = req.body.status;
}
// save updated todo in db
foundTodo.save(function (err, savedTodo) {
if (err) {
res.status(500).json({ error: err.message });
} else {
console.log(savedTodo)
res.json(savedTodo);
}
});
}
});
});
// delete todo
app.delete('/api/todos/:id', function (req, res) {
// get todo id from url params (`req.params`)
var todoId = req.params.id;
// find todo in db by id and remove
Todo.findOneAndRemove({ _id: todoId }, function (err, deletedTodo) {
if (err) {
res.status(500).json({ error: err.message });
} else {
res.json(deletedTodo);
}
});
});
const port = process.env.PORT || 3000;
/**********
* SERVER *
**********/
// listen on port 3000
app.listen(port, function() {
console.log('server started');
});