-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
60 lines (52 loc) · 1.65 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Require our dependencies
var http = require('http');
var express = require('express');
var logger = require('morgan');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var models = require('./public/scripts/models.js');
// Create an express instance and set a port variable
var app = express();
var port = process.env.PORT || 8080;
// Set application stuff
app.use(logger('dev')); // Pretty logging
app.use(bodyParser.json()); // application/json
app.use(bodyParser.urlencoded({ extended: true })); // application/x-www-form-urlencoded
app.use("/", express.static(__dirname + "/public/")); // Set /public as our static content dir
// Initialise the database connection
mongoose.connect('mongodb://localhost/todo');
// Our API
app.get('/api/todo', function (req, res) {
models.Todo.find().exec(function (err, todos) {
if (err) {
res.status(500).send(err);
}
res.json(todos);
})
});
app.post('/api/todo', function (req, res) {
var newTodo = models.Todo(req.body);
newTodo.save(function (err) {
if (err) {
res.status(500).send(err);
}
res.send();
})
});
app.post('/api/todo/:id/:action', function (req, res) {
var query = models.Todo.where({_id: mongoose.Types.ObjectId(req.params.id)});
query.findOne(function (err, todo) {
switch (req.params.action) {
case 'complete':
todo.complete(req.body.datetimeCompleted);
break;
case 'uncomplete':
todo.uncomplete();
}
res.send();
});
});
// Fire this baby up
var server = http.createServer(app).listen(port, function() {
console.log('Express server listening on http://localhost:' + port);
});