Skip to content

Commit

Permalink
adding final file structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Sevilleja committed Nov 13, 2013
1 parent 14b10e1 commit 5d6ddd0
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 69 deletions.
6 changes: 6 additions & 0 deletions app/models/todo.js
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
});
62 changes: 62 additions & 0 deletions app/routes.js
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)
});
};
72 changes: 3 additions & 69 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down

0 comments on commit 5d6ddd0

Please sign in to comment.