diff --git a/app/models/todo.js b/app/models/todo.js new file mode 100644 index 000000000..fc480331d --- /dev/null +++ b/app/models/todo.js @@ -0,0 +1,6 @@ +var mongoose = require('mongoose'); + +module.exports = mongoose.model('Todo', { + text : String, + done : Boolean +}); \ No newline at end of file diff --git a/app/routes.js b/app/routes.js index e69de29bb..a06bd30f6 100644 --- a/app/routes.js +++ b/app/routes.js @@ -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) + }); +}; \ No newline at end of file diff --git a/server.js b/server.js index 50d47def0..9f8736fac 100644 --- a/server.js +++ b/server.js @@ -2,11 +2,8 @@ var express = require('express'); var app = express(); // create our app w/ express var mongoose = require('mongoose'); // mongoose for mongodb - -var port = process.env.PORT || 8080; - -// load the database config -var database = require('./config/database'); +var port = process.env.PORT || 8080; // set the port +var database = require('./config/database'); // load the database config // configuration =============================================================== mongoose.connect(database.url); // connect to mongoDB database on modulus.io @@ -18,71 +15,8 @@ app.configure(function() { app.use(express.methodOverride()); // simulate DELETE and PUT }); -// define model ================================================================ -var Todo = mongoose.model('Todo', { - text : String, - done : Boolean -}); - // routes ====================================================================== - - // 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) - }); +require('./app/routes.js')(app); // listen (start app with node server.js) ====================================== app.listen(port);