-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
50 lines (48 loc) · 1.59 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
var http = require('http');
var fs = require('fs');
var port = 8000;
console.log("Server running at http://localhost:" + port);
var index1 = fs.readFileSync(__dirname + '/public/index1.html');
var index2 = fs.readFileSync(__dirname + '/public/index2.html');
var server = http.createServer(handler);
server.listen(port);
var redis = require("redis");
var client = redis.createClient();
var item="";
function handler(req,res){
if (req.method === 'POST') {
var body = '';
req.on('data', function (dataChunk) {
body += dataChunk;
});
req.on('end', function () {
var entries = body.split('&');
var deets = entries.map(splitByEquals);
client.incr('userCount', function(err, userCount){
var id = userCount;
client.HSET('user:' + id, "name", deets[0][1], redis.print);
client.HSET('user:' + id, "surname", deets[1][1], redis.print);
client.HSET('user:' + id, "email", deets[2][1],redis.print);
});
});
} else if (req.method === 'GET') {
res.writeHead(200, {"Content-Type": "text/html"});
client.get('userCount', function(err, reply){
res.write(index1);
var userCount = reply;
for (var i = 1; i <= userCount; i++){
var count = 1;
count ++;
client.HGETALL('user:'+i, function (err,reply){
res.write('<li>' + reply.name + ', ' + reply.surname + ', ' + reply.email.replace(/%40/, '@') + '</li>');
if (count === userCount) {
res.end(index2);
}
});
}
});
}
}
function splitByEquals(arg){
return arg.split("=");
}