-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
107 lines (96 loc) · 2.36 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
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
var express = require('express');
var app = express();
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static('app'));
app.use('/slides', express.static('slides'));
var cards = [
{
id: '1',
title: 'Card improvement',
description: 'Cards needs to be improved...some day, maybe',
cardStatus: 'TODO',
severity: 'trivial',
user: 'davidg@tid.es',
date: new Date('2013-10-09')
},
{
id: '2',
title: 'Card Super bug',
description: 'Very important bug CAUTION!!',
cardStatus: 'TODO',
severity: 'critical',
user: 'jjmr@tid.es',
date: new Date('2013-10-10')
},
{
id: '3',
title: 'Write some tests',
description: 'Some tests need to be written',
cardStatus: 'DOING',
severity: 'minor',
user: 'pjm@tid.es',
date: new Date('2013-10-13')
}
];
var idCount = 3;
function getCard(id) {
var cardMatch;
cards.forEach(function(card) {
if (card.id === id) {
cardMatch = card;
}
});
return cardMatch;
}
app.get('/cards', function(req, res) {
res.write(JSON.stringify(cards));
res.end();
});
app.get('/cards/:id', function(req, res, next) {
var card = getCard(req.params.id);
if (card) {
res.write(JSON.stringify(card));
res.end();
} else {
next();
}
});
app.post('/cards', function(req, res) {
idCount = idCount + 1;
req.body.id = idCount;
cards.push(req.body);
res.write(JSON.stringify(req.body));
res.end();
});
app.post('/cards/:id', function(req, res, next) {
var card = getCard(req.params.id);
if (card) {
cards.splice(cards.indexOf(card), 1, req.body);
res.write(JSON.stringify(req.body));
res.end()
} else {
next();
}
});
app.put('/cards/:id', function(req, res, next) {
var card = getCard(req.params.id);
if (card) {
cards.splice(cards.indexOf(card), 1, req.body);
res.write(JSON.stringify(req.body));
res.end()
} else {
next();
}
});
app.delete('/cards/:id', function(req, res, next) {
var card = getCard(req.params.id);
if (card) {
cards.splice(cards.indexOf(card), 1);
res.end()
} else {
next();
}
});
app.listen(8000);