-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Chris Sevilleja
committed
Nov 13, 2013
1 parent
14b10e1
commit 5d6ddd0
Showing
3 changed files
with
71 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
var mongoose = require('mongoose'); | ||
|
||
module.exports = mongoose.model('Todo', { | ||
text : String, | ||
done : Boolean | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
var Todo = require('./models/todo'); | ||
|
||
module.exports = function(app) { | ||
|
||
// api --------------------------------------------------------------------- | ||
// get all todos | ||
app.get('/api/todos', function(req, res) { | ||
|
||
// use mongoose to get all todos in the database | ||
Todo.find(function(err, todos) { | ||
|
||
// if there is an error retrieving, send the error. nothing after res.send(err) will execute | ||
if (err) | ||
res.send(err) | ||
|
||
res.json(todos); // return all todos in JSON format | ||
}); | ||
}); | ||
|
||
// create todo and send back all todos after creation | ||
app.post('/api/todos', function(req, res) { | ||
|
||
// create a todo, information comes from AJAX request from Angular | ||
Todo.create({ | ||
text : req.body.text, | ||
done : false | ||
}, function(err, todo) { | ||
if (err) | ||
res.send(err); | ||
|
||
// get and return all the todos after you create another | ||
Todo.find(function(err, todos) { | ||
if (err) | ||
res.send(err) | ||
res.json(todos); | ||
}); | ||
}); | ||
|
||
}); | ||
|
||
// delete a todo | ||
app.delete('/api/todos/:todo_id', function(req, res) { | ||
Todo.remove({ | ||
_id : req.params.todo_id | ||
}, function(err, todo) { | ||
if (err) | ||
res.send(err); | ||
|
||
// get and return all the todos after you create another | ||
Todo.find(function(err, todos) { | ||
if (err) | ||
res.send(err) | ||
res.json(todos); | ||
}); | ||
}); | ||
}); | ||
|
||
// application ------------------------------------------------------------- | ||
app.get('*', function(req, res) { | ||
res.sendfile('./public/index.html'); // load the single view file (angular will handle the page changes on the front-end) | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters