forked from coridrew/nodejs-todo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
85 lines (63 loc) · 2.02 KB
/
Copy pathserver.js
File metadata and controls
85 lines (63 loc) · 2.02 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
var express = require('express');
var _ = require('underscore');
var app = express();
var server = require('http').createServer(app);
var config = require('./lib/config');
var secret = require('./lib/secret');
var MongoClient = require('mongodb').MongoClient
, ObjectID = require('mongodb').ObjectID;
var collection = null;
MongoClient.connect(secret.mongoDBconnection, function (err, db) {
if (err) {
throw err;
} else {
collection = db.collection('todos');
}
//db.close();
});
app.set('view engine', 'ejs');
app.set('view options', { layout: false });
app.use('/public', express.static('public'));
app.use(express.methodOverride());
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({secret: guid()}));
app.use(app.router);
//helper method for writing out json payloads
var json = function(res, data) {
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
if(typeof data === "string") res.write(data);
else res.write(JSON.stringify(data));
res.end();
};
function guid() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
}
app.get('/', function (req, res) {
res.render('index');
});
app.get('/todos', function(req, res) {
collection.find().toArray(function(err, data) {
json(res, data);
});
});
app.post('/todos/create', function(req, res) {
collection.insert({description:req.body.description},{}, function(err, docs) {
json(res, { id: docs._id });
});
});
app.post('/todos/update', function(req, res) {
collection.update({_id:ObjectID(req.body.id)}, {$set:{description:req.body.description}});
json(res, { });
});
app.post('/todos/delete', function(req, res) {
collection.remove({_id:ObjectID(req.body.id)}, function(err, result) {
if(err)
console.log(err);
json(res, result);
});
});
server.listen(process.env.PORT || config.port);