-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.js
84 lines (74 loc) · 2.47 KB
/
handler.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
var serve = (function() {
var http = require('http');
var fs = require('fs');
var port = process.env.PORT || 8000;
console.log("Server running at http://localhost:" + port);
var redis = require("redis");
var client = redis.createClient(process.env.REDIS_URL, {no_ready_check: true});
var index = fs.readFileSync(__dirname + '/public/index.html');
function handler(req, res) {
var url = req.url;
console.log(req.method);
console.log(url);
if (url === '/') {
res.writeHead(200, {"Content-Type": "text/html"});
res.end(index);
} else if (url === '/allposts') {
(function getData() {
client.GET("tweetcount", function(err,reply){
console.log("countERROR", err, "countOBJECT", reply);
var endLength = reply;
var startLength = reply - 5;
client.LRANGE("dates", startLength, endLength, function(err,reply){
console.log("datesERROR", err, "datesOBJECT", reply);
var dates = reply;
client.multi()
.hgetall(dates[5])
.hgetall(dates[4])
.hgetall(dates[3])
.hgetall(dates[2])
.hgetall(dates[1])
.hgetall(dates[0])
.exec(function(err,replies){
console.log(replies);
res.write(JSON.stringify(replies));
res.end();
});
});
});
}());
} else if (url.indexOf('.') > -1) {
var ext = url.split('.')[1];
res.writeHead(200, {"Content-Type": "text/" + ext});
res.end(fs.readFileSync(__dirname + url));
} else if (req.method === 'POST') {
var dataInputs = url.split('/');
var date = dataInputs[1];
var username = dataInputs[2];
var tweet = dataInputs[3];
writingToDB(date, username, tweet,function(err, reply){
console.log("ERROR", err, "REPLY", reply);
res.writeHead(200, {"Content-Type": "text/html"});
res.end(reply);
});
}
}
function writingToDB(date, username, tweet, callback) {
client.incr('tweetcount', function(err, tweetCount){
client.HMSET(date, ["username", username, "tweet", tweet], callback);
client.RPUSH("dates", date);
});
}
var create = function (){
var server = http.createServer(handler);
server.listen(port);
};
return {
handler: handler,
create: create,
client: client,
writingToDB: writingToDB
//getData: getData
};
}());
module.exports = serve;