diff --git a/README.md b/README.md index 301c768ba..5a83fc441 100644 --- a/README.md +++ b/README.md @@ -7,11 +7,13 @@ Node provides the RESTful API. Angular provides the frontend and accesses the AP ## Requirements - [Node and npm](http://nodejs.org) +- MongoDB: Make sure you have your own local or remote MongoDB database URI configured in `config/database.js` ## Installation 1. Clone the repository: `git clone git@github.com:scotch-io/node-todo` 2. Install the application: `npm install` +3. Place your own MongoDB URI in `config/database.js` 3. Start the server: `node server.js` 4. View in browser at `http://localhost:8080` @@ -21,11 +23,8 @@ This repo corresponds to the Node Todo Tutorial Series on [scotch.io](http://sco Each branch represents a certain tutorial. - tut1-starter: [Creating a Single Page Todo App with Node and Angular](http://scotch.io/tutorials/javascript/creating-a-single-page-todo-app-with-node-and-angular) -- tut2-services: Coming Soon -- tut3-auth: Coming Soon -- tut4-sockets: Coming Soon -- tut5-redis: Coming Soon -- tut6-organization: Coming Soon +- tut2-organization: [Application Organization and Structure](https://scotch.io/tutorials/node-and-angular-to-do-app-application-organization-and-structure) +- tut3-services: [Controllers and Services](https://scotch.io/tutorials/node-and-angular-to-do-app-controllers-and-services) Happy Todo-ing! diff --git a/app/models/todo.js b/app/models/todo.js index fc480331d..770f42a6f 100644 --- a/app/models/todo.js +++ b/app/models/todo.js @@ -1,6 +1,8 @@ var mongoose = require('mongoose'); module.exports = mongoose.model('Todo', { - text : String, - done : Boolean + text: { + type: String, + default: '' + } }); \ No newline at end of file diff --git a/app/routes.js b/app/routes.js index a06bd30f6..b78dbe4fa 100644 --- a/app/routes.js +++ b/app/routes.js @@ -1,62 +1,57 @@ 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) - }); -}; \ No newline at end of file +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) + }); +}; diff --git a/config/database.js b/config/database.js index ed8735ffa..1f4cfc593 100644 --- a/config/database.js +++ b/config/database.js @@ -1,5 +1,4 @@ module.exports = { - - // the database url to connect - url : 'mongodb://node:node@mongo.onmodulus.net:27017/uwO3mypu' -} \ No newline at end of file + remoteUrl : 'mongodb://node:nodeuser@mongo.onmodulus.net:27017/uwO3mypu', + localUrl: 'mongodb://localhost/meanstacktutorials' +}; diff --git a/license b/license new file mode 100644 index 000000000..766a0a595 --- /dev/null +++ b/license @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) {{{year}}} {{{fullname}}} + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/package.json b/package.json index 0f84bec6d..b4584b750 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,14 @@ { - "name" : "node-todo", - "version" : "0.0.0", - "description" : "Simple todo application.", - "main" : "server.js", - "author" : "Scotch", - "dependencies" : { - "express" : "~3.4.4", - "mongoose" : "~3.6.2" + "name": "node-todo", + "version": "0.0.1", + "description": "Simple todo application.", + "main": "server.js", + "author": "Scotch", + "dependencies": { + "body-parser": "^1.4.3", + "express": "^4.13.4", + "method-override": "^2.1.3", + "mongoose": "^4.4.12", + "morgan": "^1.1.1" } } diff --git a/public/core.js b/public/core.js deleted file mode 100644 index 510e43861..000000000 --- a/public/core.js +++ /dev/null @@ -1,38 +0,0 @@ -var scotchTodo = angular.module('scotchTodo', []); - -function mainController($scope, $http) { - $scope.formData = {}; - - // when landing on the page, get all todos and show them - $http.get('/api/todos') - .success(function(data) { - $scope.todos = data; - }) - .error(function(data) { - console.log('Error: ' + data); - }); - - // when submitting the add form, send the text to the node API - $scope.createTodo = function() { - $http.post('/api/todos', $scope.formData) - .success(function(data) { - $scope.formData.text = ''; - $scope.todos = data; - }) - .error(function(data) { - console.log('Error: ' + data); - }); - }; - - // delete a todo after checking it - $scope.deleteTodo = function(id) { - $http.delete('/api/todos/' + id) - .success(function(data) { - $scope.todos = data; - }) - .error(function(data) { - console.log('Error: ' + data); - }); - }; - -} \ No newline at end of file diff --git a/public/index.html b/public/index.html index 4ededaff8..3bb866403 100644 --- a/public/index.html +++ b/public/index.html @@ -11,6 +11,7 @@ +