-
Notifications
You must be signed in to change notification settings - Fork 2
/
handlers.js
64 lines (55 loc) · 1.61 KB
/
handlers.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
var fs = require('fs');
var database = require('./redisAdaptor.js');
function handlers(config) {
var Model = database({
connection: config.connection
});
return {
'POST /addClap': function(req, res) {
var newClap;
req.on('data', function(chunk) {
newClap = chunk + ''; //turns clap input box buffer into text
if(newClap.indexOf("<") > -1 || newClap.indexOf(">") > -1) {
newClap = newClap.replace(/</g, "<").replace(/>/g, ">");
}
});
req.on('end', function() {
newClap = JSON.parse(newClap);
newClap.time = new Date().getTime();
Model.create(newClap, function(clap) {
res.writeHead(200, "OK", {'Content-Type': 'text/html'});
res.end(JSON.stringify(clap)); //sends back new tweet for display
});
});
},
'GET /allClaps': function(req, res) {
Model.read(function(data){
res.end(JSON.stringify(data));
});
},
'POST /delete': function(req, res) {
var clapId;
req.on('data', function(chunk) {
clapId = chunk + ''; //turns clap input box buffer into text
});
req.on('end', function() {
Model.delete(clapId, function(reply){
res.end(reply.toString());
});
});
},
generic: function(req, res) {
fs.readFile(__dirname + req.url, function(err, data){
if (err){
res.end();
}
else {
var ext = req.url.split('.')[1];
res.writeHead(200, {'Content-Type' : 'text/' + ext});
res.end(data);
}
});
}
};
}
module.exports = handlers;