-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Setup local Mongo instance config and remote config. Updated dependen…
…cies.
- Loading branch information
Showing
5 changed files
with
74 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: '' | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters