-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.js
58 lines (50 loc) · 1.74 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
var fs = require('fs');
var index1 = fs.readFileSync(__dirname + '/public/index1.html');
var index2 = fs.readFileSync(__dirname + '/public/index2.html');
var redis = require('redis');
var client = redis.createClient();
function handler(request, response) {
var url = request.url;
if (request.method === "GET") {
client.lrange("favourites", 0, -1, function(err, reply) {
response.writeHead(200, {"Content-Type": "text/html"});
response.write(index1);
response.end(index2);
});
}
else if (request.method === "POST") {
var body ='';
request.on('data', function(chunk){
body += chunk;
item = "<li>" + body.split('item=').join('').split(',').join('') + "</li>";
console.log(item);
client.rpush('favourites', item, function (err,reply) {
if(err){
console.log('error from client rpush');
}
else{
client.lrange("favourites", 0, -1, function(err, reply) {
response.writeHead(200, {"Content-Type": "text/html"});
console.log(reply);
response.write(index1);
console.log(reply.join(''));
response.write(reply.join(''));
response.end(index2);
});
}
});
});
}
else {
fs.readFile(__dirname + request.url, function(err, file) {
if (err) {
response.writeHead(404, {"Content-Type": "text/" + ext});
} else {
var ext = request.url.split('.')[1];
response.writeHead(200, {"Content-Type": "text/" + ext});
response.end(file);
}
});
}
}
module.exports = handler;