diff options
| author | Kumar Damani <kumar.damani@mail.utoronto.ca> | 2016-08-03 18:30:06 +0000 |
|---|---|---|
| committer | Kumar Damani <kumar.damani@mail.utoronto.ca> | 2016-08-03 18:30:06 +0000 |
| commit | f383a85f8ed569821ea0e929139dcd68c5541519 (patch) | |
| tree | e21ca3c635e0bf6aaa124a7954451a09237121c4 /app | |
all files
Diffstat (limited to 'app')
| -rw-r--r-- | app/models/todo.js | 8 | ||||
| -rw-r--r-- | app/routes.js | 56 |
2 files changed, 64 insertions, 0 deletions
diff --git a/app/models/todo.js b/app/models/todo.js new file mode 100644 index 0000000..02f4088 --- /dev/null +++ b/app/models/todo.js @@ -0,0 +1,8 @@ +var mongoose = require('mongoose'); + +module.exports = mongoose.model('todoCollect', { + text: { + type: String, + default: '' + } +});
\ No newline at end of file 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 |
