-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
266 lines (218 loc) · 6.11 KB
/
app.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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/* 2001-05-25 (mca) : collection+json */
/* Designing Hypermedia APIs by Mike Amundsen (2011) */
/**
* Module dependencies.
*/
// for express
var express = require('express');
var app = module.exports = express.createServer();
// for couch
var cradle = require('cradle');
var db = new(cradle.Connection)().database('collection-data-tasks');
// global data
var contentType = 'application/json';
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// register custom media type as a JSON format
express.bodyParser.parse['application/collection+json'] = JSON.parse;
// Routes
/* handle default task list */
app.get('/collection/tasks/', function(req, res){
var view = '/_design/example/_view/due_date';
db.get(view, function (err, doc) {
res.header('content-type',contentType);
res.render('tasks', {
site : 'http://localhost:3000/collection/tasks/',
items : doc
});
});
});
/* filters */
app.get('/collection/tasks/;queries', function(req, res){
res.header('content-type',contentType);
res.render('queries', {
layout : 'item-layout',
site : 'http://localhost:3000/collection/tasks/'
});
});
app.get('/collection/tasks/;template', function(req, res){
res.header('content-type',contentType);
res.render('template', {
layout : 'item-layout',
site : 'http://localhost:3000/collection/tasks/'
});
});
app.get('/collection/tasks/;all', function(req, res){
var view = '/_design/example/_view/all';
db.get(view, function (err, doc) {
res.header('content-type',contentType);
res.render('tasks', {
site : 'http://localhost:3000/collection/tasks/',
items : doc
});
});
});
app.get('/collection/tasks/;open', function(req, res){
var view = '/_design/example/_view/open';
db.get(view, function (err, doc) {
res.header('content-type',contentType);
res.render('tasks', {
site : 'http://localhost:3000/collection/tasks/',
items : doc
});
});
});
app.get('/collection/tasks/;closed', function(req, res){
var view = '/_design/example/_view/closed';
db.get(view, function (err, doc) {
res.header('content-type',contentType);
res.render('tasks', {
site : 'http://localhost:3000/collection/tasks/',
items : doc
});
});
});
app.get('/collection/tasks/;date-range', function(req, res){
var d1 = (req.query['date-start'] || '');
var d2 = (req.query['date-stop'] || '');
var options = {};
options.startkey=String.fromCharCode(34)+d1+String.fromCharCode(34);
options.endkey=String.fromCharCode(34)+d2+String.fromCharCode(34);
var view = '/_design/example/_view/due_date';
db.get(view, options, function (err, doc) {
res.header('content-type',contentType);
res.render('tasks', {
site : 'http://localhost:3000/collection/tasks/',
items : doc,
query : view
});
});
});
/* handle single task item */
app.get('/collection/tasks/:i', function(req, res){
var view = '/'+req.params.i;
db.get(view, function (err, doc) {
res.header('content-type',contentType);
res.header('etag',doc._rev);
res.render('task', {
layout : 'item-layout',
site : 'http://localhost:3000/collection/tasks/',
item : doc
});
});
});
/* handle creating a new task */
app.post('/collection/tasks/', function(req, res){
var description, completed, dateDue, data, i, x;
// get data array
data = req.body.template.data;
// pull out values we want
for(i=0,x=data.length;i<x;i++) {
switch(data[i].name) {
case 'description' :
description = data[i].value;
break;
case 'completed' :
completed = data[i].value;
break;
case 'dateDue' :
dateDue = data[i].value;
break;
}
}
// build JSON to write
var item = {};
item.description = description;
item.completed = completed;
item.dateDue = dateDue;
item.dateCreated = today();
// write to DB
db.save(item, function(err, doc) {
if(err) {
res.status=400;
res.send(err);
}
else {
res.redirect('/collection/tasks/', 302);
}
});
});
/* handle updating an existing task item */
app.put('/collection/tasks/:i', function(req, res) {
var idx = (req.params.i || '');
var rev = req.header("if-match", "*");
var description, completed, dateDue, data, i, x;
// get data array
data = req.body.template.data;
// pull out values we want
for(i=0,x=data.length;i<x;i++) {
switch(data[i].name) {
case 'description' :
description = data[i].value;
break;
case 'completed' :
completed = data[i].value;
break;
case 'dateDue' :
dateDue = data[i].value;
break;
}
}
// build JSON to write
var item = {};
item.description = description;
item.completed = completed;
item.dateDue = dateDue;
item.dateCreated = today();
db.save(idx, rev, item, function (err, doc) {
// return the same item
res.redirect('/collection/tasks/'+idx, 302);
});
});
/* handle deleting existing task */
app.delete('/collection/tasks/:i', function(req, res) {
var idx = (req.params.i || '');
var rev = req.header("if-match", "*");
db.remove(idx, rev, function (err, doc) {
if(err) {
res.status=400;
res.send(err);
}
else {
res.status= 204;
res.send();
}
});
});
function today() {
var y, m, d, dt;
dt = new Date();
y = dt.getFullYear();
m = dt.getMonth()+1;
if(m.length===1) {
m = '0'+m;
}
d = dt.getDate();
if(d.length===1) {
d = '0'+d;
}
return y+'-'+m+'-'+d;
}
// Only listen on $ node app.js
if (!module.parent) {
app.listen(3000);
console.log("Express server listening on port %d", app.address().port);
}