Skip to content

Commit 536a58a

Browse files
committed
Setup local Mongo instance config and remote config. Updated dependencies.
1 parent 5f31fd7 commit 536a58a

File tree

5 files changed

+74
-70
lines changed

5 files changed

+74
-70
lines changed

app/models/todo.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
var mongoose = require('mongoose');
22

33
module.exports = mongoose.model('Todo', {
4-
text : {type : String, default: ''}
4+
text: {
5+
type: String,
6+
default: ''
7+
}
58
});

app/routes.js

+55-54
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,58 @@
11
var Todo = require('./models/todo');
22

3-
function getTodos(res){
4-
Todo.find(function(err, todos) {
5-
6-
// if there is an error retrieving, send the error. nothing after res.send(err) will execute
7-
if (err)
8-
res.send(err)
9-
10-
res.json(todos); // return all todos in JSON format
11-
});
12-
};
13-
14-
module.exports = function(app) {
15-
16-
// api ---------------------------------------------------------------------
17-
// get all todos
18-
app.get('/api/todos', function(req, res) {
19-
20-
// use mongoose to get all todos in the database
21-
getTodos(res);
22-
});
23-
24-
// create todo and send back all todos after creation
25-
app.post('/api/todos', function(req, res) {
26-
27-
// create a todo, information comes from AJAX request from Angular
28-
Todo.create({
29-
text : req.body.text,
30-
done : false
31-
}, function(err, todo) {
32-
if (err)
33-
res.send(err);
34-
35-
// get and return all the todos after you create another
36-
getTodos(res);
37-
});
38-
39-
});
40-
41-
// delete a todo
42-
app.delete('/api/todos/:todo_id', function(req, res) {
43-
Todo.remove({
44-
_id : req.params.todo_id
45-
}, function(err, todo) {
46-
if (err)
47-
res.send(err);
48-
49-
getTodos(res);
50-
});
51-
});
52-
53-
// application -------------------------------------------------------------
54-
app.get('*', function(req, res) {
55-
res.sendfile('./public/index.html'); // load the single view file (angular will handle the page changes on the front-end)
56-
});
3+
function getTodos(res) {
4+
Todo.find(function (err, todos) {
5+
6+
// if there is an error retrieving, send the error. nothing after res.send(err) will execute
7+
if (err) {
8+
res.send(err);
9+
}
10+
11+
res.json(todos); // return all todos in JSON format
12+
});
13+
}
14+
;
15+
16+
module.exports = function (app) {
17+
18+
// api ---------------------------------------------------------------------
19+
// get all todos
20+
app.get('/api/todos', function (req, res) {
21+
// use mongoose to get all todos in the database
22+
getTodos(res);
23+
});
24+
25+
// create todo and send back all todos after creation
26+
app.post('/api/todos', function (req, res) {
27+
28+
// create a todo, information comes from AJAX request from Angular
29+
Todo.create({
30+
text: req.body.text,
31+
done: false
32+
}, function (err, todo) {
33+
if (err)
34+
res.send(err);
35+
36+
// get and return all the todos after you create another
37+
getTodos(res);
38+
});
39+
40+
});
41+
42+
// delete a todo
43+
app.delete('/api/todos/:todo_id', function (req, res) {
44+
Todo.remove({
45+
_id: req.params.todo_id
46+
}, function (err, todo) {
47+
if (err)
48+
res.send(err);
49+
50+
getTodos(res);
51+
});
52+
});
53+
54+
// application -------------------------------------------------------------
55+
app.get('*', function (req, res) {
56+
res.sendFile(__dirname + '/public/index.html'); // load the single view file (angular will handle the page changes on the front-end)
57+
});
5758
};

config/database.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
module.exports = {
2-
3-
// the database url to connect
4-
url : 'mongodb://node:[email protected]:27017/uwO3mypu'
5-
}
2+
remoteUrl : 'mongodb://node:[email protected]:27017/uwO3mypu',
3+
localUrl: 'mongodb://localhost/meanstacktutorials'
4+
};

package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
{
22
"name" : "node-todo",
3-
"version" : "0.0.0",
3+
"version" : "0.0.1",
44
"description" : "Simple todo application.",
55
"main" : "server.js",
66
"author" : "Scotch",
77
"dependencies" : {
8-
"express" : "~4.6.1",
8+
"express" : "~4.13.4",
99
"mongoose" : "~3.8.13",
1010
"morgan" : "~1.1.1",
1111
"body-parser" : "~1.4.3",
12-
"method-override" : "~2.1.1"
12+
"method-override" : "~2.1.1",
13+
"electrolyte" : "~0.2.0"
1314
}
1415
}

server.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
// set up ======================================================================
2-
var express = require('express');
3-
var app = express(); // create our app w/ express
4-
var mongoose = require('mongoose'); // mongoose for mongodb
5-
var port = process.env.PORT || 8080; // set the port
2+
var express = require('express');
3+
var app = express(); // create our app w/ express
4+
var mongoose = require('mongoose'); // mongoose for mongodb
5+
var port = process.env.PORT || 8080; // set the port
66
var database = require('./config/database'); // load the database config
7-
var morgan = require('morgan');
7+
var morgan = require('morgan');
88
var bodyParser = require('body-parser');
99
var methodOverride = require('method-override');
1010

1111
// configuration ===============================================================
12-
mongoose.connect(database.url); // connect to mongoDB database on modulus.io
12+
mongoose.connect(database.localUrl); // Connect to local MongoDB instance. A remoteUrl is also available (modulus.io)
1313

1414
app.use(express.static(__dirname + '/public')); // set the static files location /public/img will be /img for users
1515
app.use(morgan('dev')); // log every request to the console
16-
app.use(bodyParser.urlencoded({'extended':'true'})); // parse application/x-www-form-urlencoded
16+
app.use(bodyParser.urlencoded({'extended': 'true'})); // parse application/x-www-form-urlencoded
1717
app.use(bodyParser.json()); // parse application/json
18-
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json
18+
app.use(bodyParser.json({type: 'application/vnd.api+json'})); // parse application/vnd.api+json as json
1919
app.use(methodOverride('X-HTTP-Method-Override')); // override with the X-HTTP-Method-Override header in the request
2020

2121

0 commit comments

Comments
 (0)