Skip to content

Commit 5d6ddd0

Browse files
author
Chris Sevilleja
committed
adding final file structure
1 parent 14b10e1 commit 5d6ddd0

File tree

3 files changed

+71
-69
lines changed

3 files changed

+71
-69
lines changed

app/models/todo.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var mongoose = require('mongoose');
2+
3+
module.exports = mongoose.model('Todo', {
4+
text : String,
5+
done : Boolean
6+
});

app/routes.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
var Todo = require('./models/todo');
2+
3+
module.exports = function(app) {
4+
5+
// api ---------------------------------------------------------------------
6+
// get all todos
7+
app.get('/api/todos', function(req, res) {
8+
9+
// use mongoose to get all todos in the database
10+
Todo.find(function(err, todos) {
11+
12+
// if there is an error retrieving, send the error. nothing after res.send(err) will execute
13+
if (err)
14+
res.send(err)
15+
16+
res.json(todos); // return all todos in JSON format
17+
});
18+
});
19+
20+
// create todo and send back all todos after creation
21+
app.post('/api/todos', function(req, res) {
22+
23+
// create a todo, information comes from AJAX request from Angular
24+
Todo.create({
25+
text : req.body.text,
26+
done : false
27+
}, function(err, todo) {
28+
if (err)
29+
res.send(err);
30+
31+
// get and return all the todos after you create another
32+
Todo.find(function(err, todos) {
33+
if (err)
34+
res.send(err)
35+
res.json(todos);
36+
});
37+
});
38+
39+
});
40+
41+
// delete a todo
42+
app.delete('/api/todos/:todo_id', function(req, res) {
43+
Todo.remove({
44+
_id : req.params.todo_id
45+
}, function(err, todo) {
46+
if (err)
47+
res.send(err);
48+
49+
// get and return all the todos after you create another
50+
Todo.find(function(err, todos) {
51+
if (err)
52+
res.send(err)
53+
res.json(todos);
54+
});
55+
});
56+
});
57+
58+
// application -------------------------------------------------------------
59+
app.get('*', function(req, res) {
60+
res.sendfile('./public/index.html'); // load the single view file (angular will handle the page changes on the front-end)
61+
});
62+
};

server.js

Lines changed: 3 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22
var express = require('express');
33
var app = express(); // create our app w/ express
44
var mongoose = require('mongoose'); // mongoose for mongodb
5-
6-
var port = process.env.PORT || 8080;
7-
8-
// load the database config
9-
var database = require('./config/database');
5+
var port = process.env.PORT || 8080; // set the port
6+
var database = require('./config/database'); // load the database config
107

118
// configuration ===============================================================
129
mongoose.connect(database.url); // connect to mongoDB database on modulus.io
@@ -18,71 +15,8 @@ app.configure(function() {
1815
app.use(express.methodOverride()); // simulate DELETE and PUT
1916
});
2017

21-
// define model ================================================================
22-
var Todo = mongoose.model('Todo', {
23-
text : String,
24-
done : Boolean
25-
});
26-
2718
// routes ======================================================================
28-
29-
// api ---------------------------------------------------------------------
30-
// get all todos
31-
app.get('/api/todos', function(req, res) {
32-
33-
// use mongoose to get all todos in the database
34-
Todo.find(function(err, todos) {
35-
36-
// if there is an error retrieving, send the error. nothing after res.send(err) will execute
37-
if (err)
38-
res.send(err)
39-
40-
res.json(todos); // return all todos in JSON format
41-
});
42-
});
43-
44-
// create todo and send back all todos after creation
45-
app.post('/api/todos', function(req, res) {
46-
47-
// create a todo, information comes from AJAX request from Angular
48-
Todo.create({
49-
text : req.body.text,
50-
done : false
51-
}, function(err, todo) {
52-
if (err)
53-
res.send(err);
54-
55-
// get and return all the todos after you create another
56-
Todo.find(function(err, todos) {
57-
if (err)
58-
res.send(err)
59-
res.json(todos);
60-
});
61-
});
62-
63-
});
64-
65-
// delete a todo
66-
app.delete('/api/todos/:todo_id', function(req, res) {
67-
Todo.remove({
68-
_id : req.params.todo_id
69-
}, function(err, todo) {
70-
if (err)
71-
res.send(err);
72-
73-
// get and return all the todos after you create another
74-
Todo.find(function(err, todos) {
75-
if (err)
76-
res.send(err)
77-
res.json(todos);
78-
});
79-
});
80-
});
81-
82-
// application -------------------------------------------------------------
83-
app.get('*', function(req, res) {
84-
res.sendfile('./public/index.html'); // load the single view file (angular will handle the page changes on the front-end)
85-
});
19+
require('./app/routes.js')(app);
8620

8721
// listen (start app with node server.js) ======================================
8822
app.listen(port);

0 commit comments

Comments
 (0)