diff --git a/app/models/todo.js b/app/models/todo.js index 082a2ee37..770f42a6f 100644 --- a/app/models/todo.js +++ b/app/models/todo.js @@ -1,5 +1,8 @@ var mongoose = require('mongoose'); module.exports = mongoose.model('Todo', { - text : {type : String, default: ''} + text: { + type: String, + default: '' + } }); \ No newline at end of file diff --git a/app/routes.js b/app/routes.js index 17a201567..ac14eb29c 100644 --- a/app/routes.js +++ b/app/routes.js @@ -1,57 +1,58 @@ var Todo = require('./models/todo'); -function getTodos(res){ - 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 - }); -}; - -module.exports = function(app) { - - // api --------------------------------------------------------------------- - // get all todos - app.get('/api/todos', function(req, res) { - - // use mongoose to get all todos in the database - getTodos(res); - }); - - // 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 - getTodos(res); - }); - - }); - - // 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); - - getTodos(res); - }); - }); - - // 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) - }); +function getTodos(res) { + 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 + }); +} +; + +module.exports = function (app) { + + // api --------------------------------------------------------------------- + // get all todos + app.get('/api/todos', function (req, res) { + // use mongoose to get all todos in the database + getTodos(res); + }); + + // 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 + getTodos(res); + }); + + }); + + // 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); + + getTodos(res); + }); + }); + + // application ------------------------------------------------------------- + app.get('*', function (req, res) { + res.sendFile(__dirname + '/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/config/database.js b/config/database.js index cd3586632..1f4cfc593 100644 --- a/config/database.js +++ b/config/database.js @@ -1,5 +1,4 @@ module.exports = { - - // the database url to connect - url : 'mongodb://node:nodeuser@mongo.onmodulus.net:27017/uwO3mypu' -} + remoteUrl : 'mongodb://node:nodeuser@mongo.onmodulus.net:27017/uwO3mypu', + localUrl: 'mongodb://localhost/meanstacktutorials' +}; diff --git a/package.json b/package.json index 4284578e2..b63157c30 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,15 @@ { "name" : "node-todo", - "version" : "0.0.0", + "version" : "0.0.1", "description" : "Simple todo application.", "main" : "server.js", "author" : "Scotch", "dependencies" : { - "express" : "~4.6.1", + "express" : "~4.13.4", "mongoose" : "~3.8.13", "morgan" : "~1.1.1", "body-parser" : "~1.4.3", - "method-override" : "~2.1.1" + "method-override" : "~2.1.1", + "electrolyte" : "~0.2.0" } } diff --git a/server.js b/server.js index 2bb0a9bc1..737872bae 100644 --- a/server.js +++ b/server.js @@ -1,21 +1,21 @@ // set up ====================================================================== -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; // set the port +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; // set the port var database = require('./config/database'); // load the database config -var morgan = require('morgan'); +var morgan = require('morgan'); var bodyParser = require('body-parser'); var methodOverride = require('method-override'); // configuration =============================================================== -mongoose.connect(database.url); // connect to mongoDB database on modulus.io +mongoose.connect(database.localUrl); // Connect to local MongoDB instance. A remoteUrl is also available (modulus.io) app.use(express.static(__dirname + '/public')); // set the static files location /public/img will be /img for users app.use(morgan('dev')); // log every request to the console -app.use(bodyParser.urlencoded({'extended':'true'})); // parse application/x-www-form-urlencoded +app.use(bodyParser.urlencoded({'extended': 'true'})); // parse application/x-www-form-urlencoded app.use(bodyParser.json()); // parse application/json -app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json +app.use(bodyParser.json({type: 'application/vnd.api+json'})); // parse application/vnd.api+json as json app.use(methodOverride('X-HTTP-Method-Override')); // override with the X-HTTP-Method-Override header in the request