aboutsummaryrefslogtreecommitdiff
path: root/app/routes.js
diff options
context:
space:
mode:
Diffstat (limited to 'app/routes.js')
-rw-r--r--app/routes.js56
1 files changed, 56 insertions, 0 deletions
diff --git a/app/routes.js b/app/routes.js
new file mode 100644
index 0000000..7b596dc
--- /dev/null
+++ b/app/routes.js
@@ -0,0 +1,56 @@
+// get the todos collection
+var todoCollect = require('./models/todo');
+
+
+// use mongo to get all the items in db
+function getTodos (res) {
+ todoCollect.find(function (err, todos) {
+ if (err) res.send(err);
+
+ res.json(todos); // return all todos in JSON format
+ // console.log(todos);
+ });
+}
+
+
+// ===========================API===============================
+module.exports = function (app) {
+
+ // get all items
+ app.get('/api/todos', function (req, res) {
+ getTodos(res);
+ });
+
+ // create an item
+ app.post('/api/todos', function (req, res) {
+
+ // add item to the list
+ todoCollect.create({
+ text: req.body.text,
+ done: false
+ }, function (err, todo) {
+ if (err) res.send(err);
+
+ // 'refresh' to-do list
+ getTodos(res);
+ });
+
+ });
+
+ // delete item
+ app.delete('/api/todos/:todo_id', function (req, res) {
+ todoCollect.remove({
+ _id: req.params.todo_id
+ }, function (err, todo) {
+ if (err) res.send(err);
+
+ // 'refresh' to-do list
+ getTodos(res);
+ });
+ });
+
+ // load the static html file for now
+ app.get('*', function (req, res) {
+ res.sendFile(__dirname + '/public/index.html');
+ });
+}; \ No newline at end of file