Skip to content

Commit

Permalink
Setup local Mongo instance config and remote config. Updated dependen…
Browse files Browse the repository at this point in the history
…cies.
  • Loading branch information
JSONRice committed Feb 7, 2016
1 parent 5f31fd7 commit 536a58a
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 70 deletions.
5 changes: 4 additions & 1 deletion app/models/todo.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
var mongoose = require('mongoose');

module.exports = mongoose.model('Todo', {
text : {type : String, default: ''}
text: {
type: String,
default: ''
}
});
109 changes: 55 additions & 54 deletions app/routes.js
Original file line number Diff line number Diff line change
@@ -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)
});
};
7 changes: 3 additions & 4 deletions config/database.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module.exports = {

// the database url to connect
url : 'mongodb://node:[email protected]:27017/uwO3mypu'
}
remoteUrl : 'mongodb://node:[email protected]:27017/uwO3mypu',
localUrl: 'mongodb://localhost/meanstacktutorials'
};
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
16 changes: 8 additions & 8 deletions server.js
Original file line number Diff line number Diff line change
@@ -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


Expand Down

0 comments on commit 536a58a

Please sign in to comment.