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