22var express = require ( 'express' ) ;
33var app = express ( ) ; // create our app w/ express
44var 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 ===============================================================
129mongoose . 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) ======================================
8822app . listen ( port ) ;
0 commit comments