From 536a58a734d8cab484245e92f5dc3325aae85d17 Mon Sep 17 00:00:00 2001 From: Jason Rice Date: Sat, 6 Feb 2016 21:10:17 -0500 Subject: [PATCH 1/8] Setup local Mongo instance config and remote config. Updated dependencies. --- app/models/todo.js | 5 ++- app/routes.js | 109 +++++++++++++++++++++++---------------------- config/database.js | 7 ++- package.json | 7 +-- server.js | 16 +++---- 5 files changed, 74 insertions(+), 70 deletions(-) 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 From c56600e196f726bf7f7c10912249875c59de7e2a Mon Sep 17 00:00:00 2001 From: Jason Rice Date: Mon, 8 Feb 2016 15:11:16 -0500 Subject: [PATCH 2/8] Update package.json Remove electrolyte. --- package.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/package.json b/package.json index b63157c30..244d84c08 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,6 @@ "mongoose" : "~3.8.13", "morgan" : "~1.1.1", "body-parser" : "~1.4.3", - "method-override" : "~2.1.1", - "electrolyte" : "~0.2.0" + "method-override" : "~2.1.1" } } From 4a355cefb6b60d1af0a850fc238087962709513f Mon Sep 17 00:00:00 2001 From: Chris Sevilleja Date: Tue, 12 Apr 2016 22:19:30 -0700 Subject: [PATCH 3/8] updating dependencies. close #18 --- README.md | 9 ++++----- package.json | 22 +++++++++++----------- 2 files changed, 15 insertions(+), 16 deletions(-) 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/package.json b/package.json index 244d84c08..b4584b750 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,14 @@ { - "name" : "node-todo", - "version" : "0.0.1", - "description" : "Simple todo application.", - "main" : "server.js", - "author" : "Scotch", - "dependencies" : { - "express" : "~4.13.4", - "mongoose" : "~3.8.13", - "morgan" : "~1.1.1", - "body-parser" : "~1.4.3", - "method-override" : "~2.1.1" + "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" } } From 88c7975d507fea9f01ea517de6ab093dc36376b9 Mon Sep 17 00:00:00 2001 From: Chris Sevilleja Date: Thu, 21 Jul 2016 10:32:26 -0700 Subject: [PATCH 4/8] Update server.js --- server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server.js b/server.js index 737872bae..ba5e71a5d 100644 --- a/server.js +++ b/server.js @@ -11,7 +11,7 @@ var methodOverride = require('method-override'); // configuration =============================================================== 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(express.static('./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.json()); // parse application/json From b518e83a9d3b08ea506479a7427f14423caea1b5 Mon Sep 17 00:00:00 2001 From: Nitin Tulswani Date: Mon, 19 Sep 2016 16:44:59 +0530 Subject: [PATCH 5/8] little typo --- app/routes.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/app/routes.js b/app/routes.js index ac14eb29c..b78dbe4fa 100644 --- a/app/routes.js +++ b/app/routes.js @@ -10,8 +10,7 @@ function getTodos(res) { res.json(todos); // return all todos in JSON format }); -} -; +}; module.exports = function (app) { @@ -55,4 +54,4 @@ module.exports = function (app) { 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 +}; From a98d02497181775026c3ca01a01d3be65aa4bcf3 Mon Sep 17 00:00:00 2001 From: Daryn Mitchell Date: Mon, 15 May 2017 16:15:07 +0300 Subject: [PATCH 6/8] Fix URL for first tutorial --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5a83fc441..86e344d81 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ Node provides the RESTful API. Angular provides the frontend and accesses the AP This repo corresponds to the Node Todo Tutorial Series on [scotch.io](http://scotch.io) 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) +- tut1-starter: [Creating a Single Page Todo App with Node and Angular](https://scotch.io/tutorials/creating-a-single-page-todo-app-with-node-and-angular) - 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) From b3f4b863609c88d001e6f98de804fb41fb8f32c3 Mon Sep 17 00:00:00 2001 From: Guido Date: Mon, 23 Oct 2017 09:21:39 -0300 Subject: [PATCH 7/8] updated modules versions and corrected and issue on mongoose displaying awkward messages on connect() --- package.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index b4584b750..fde172c12 100644 --- a/package.json +++ b/package.json @@ -5,10 +5,10 @@ "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" + "body-parser": "^1.18.2", + "mongoose": "4.10.8", + "express": "^4.10.8", + "method-override": "^2.3.10", + "morgan": "^1.9.0" } } From bc45ae454d7b5d4508037626a35a8fa9e7036947 Mon Sep 17 00:00:00 2001 From: Guido Date: Mon, 23 Oct 2017 09:47:26 -0300 Subject: [PATCH 8/8] Spanish translation --- README_es.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 README_es.md diff --git a/README_es.md b/README_es.md new file mode 100644 index 000000000..b75cf5188 --- /dev/null +++ b/README_es.md @@ -0,0 +1,35 @@ +###### Spanish Translation + +El siguiente codigo construye una app con MongoDB y Angular. Simplemente a modo de ejemplo y como un tutorial. + +Node, tecnologia de base en esta app, proveera la API RESTful. Angula proveera el frontendd y el acceso a la API y sus recursos. + +MongoDB sera nuestra base de datos. + +## Requerimientos +- [Node and npm](http://nodejs.org) +- MongoDB: Asegurate de tener tu instancia de MonboDB configurada correctamente en `config/databases.js` + +## Instalacion + + +1. Clonar el repositorio: `git clone git@github.com:scotch-io/node-todo` +2. Instalar la app: `npm install` +3. Configurar su base de datos en `config/database.js` +3. Inicie el servidor: `node server.js` +4. Con su navegador, dirijase a `http://localhost:8080` + +## Series de tutoriales que pueden ser interesantes o estar relacionados + +Este tutorial es parte de una serie de tutoriales en [scotch.io](http://scotch.io) + +Cada branch representa un tutorial determinado. +- tut1-starter: [Crear una Single Page Ap con Node y Angular](http://scotch.io/tutorials/javascript/creating-a-single-page-todo-app-with-node-and-angular) +- tut2-organization: [Organizacoin de la APP y su esructure](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) + +A divertirse :) + +![Todo-aholic](http://i.imgur.com/ikyqgrn.png) + +