Skip to content

Commit

Permalink
adding mailing list files
Browse files Browse the repository at this point in the history
  • Loading branch information
Jbarget committed Oct 6, 2015
0 parents commit ddbc1ce
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
dump.rdb
.DS_Store
14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "d2",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"redis": "^2.1.0"
}
}
22 changes: 22 additions & 0 deletions public/index1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html>

<head>

</head>

<body>
<h5>My Mailing List</h5>
<form action="/" method="POST">
<p>name</p>
<input type="text" placeholder="name" name="name"></input>
<br>
<p>surname</p>
<input type="text" placeholder="surname" name="surname"></input>
<br>
<p>email</p>
<input type="text" placeholder="email" name="email"></input>
<br>
<input type="submit" ></input>
</form>
<ul class="details">
6 changes: 6 additions & 0 deletions public/index2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

</ul>

</body>

</html>
29 changes: 29 additions & 0 deletions public/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

// function sortingShitOut(){
function requestingOutputFromDatabase(){
var res = new XMLHttpRequest();
res.open('GET', '/');
res.send();
return res.responseText;

// out.onreadystatechange = function(){
// if(out.readyState === 4 && out.status === 200){

// document.getElementById('favTing').innerHTML = out.
// }
};

}
//
// function gettingInputFromDatabase(){
// var out = new XMLHttpRequest();
// out.onreadystatechange(function(){
// if (out.readyState === 4 && out.status === 200){
// console.log(out.responseText);
// document.getElementsByClassName('favourite-things').innerHTML = out.responseText;
// }
// });
// out.open('GET', '/');
// out.send();
// }
// }
50 changes: 50 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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("=");
}

0 comments on commit ddbc1ce

Please sign in to comment.