Last active
May 15, 2019 18:46
-
-
Save phsamuel/e0889440b8fbaa5362283d7b8a72e6c6 to your computer and use it in GitHub Desktop.
A simple "voting" example with node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<body> | |
<H2>Are you sure to overwrite your votes?</H2> | |
<form action="" method="post" enctype="multipart/form-data"> | |
<fieldset> | |
<input type="submit" name="action" value="Update" /> | |
<input type="submit" name="action" value="Cancel" /> | |
</fieldset> | |
</form> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<body> | |
<H1>Presentation Voting for OU Deep Learning 2017</H1> | |
Please note that 5 is highest and 1 is lowest. | |
<form action="" method="post" enctype="multipart/form-data"> | |
<fieldset> | |
<label for="name">Your name:</label> | |
<select name="name" id="name"> | |
<option disabled selected>Please pick one</option> | |
<option>Aakash</option> | |
<option>Ahmad A</option> | |
<option>Ahmad M</option> | |
<option>Dong</option> | |
<option>Muhanad</option> | |
<option>Naim</option> | |
<option>Obada</option> | |
<option>Siraj</option> | |
<option>Soubhi</option> | |
<option>Tamer</option> | |
<option>Varun</option> | |
</select> | |
<label for="presenter1">Score for Aakash</label> | |
<select name="aakash" id="presenter1"> | |
<option>1</option> | |
<option>1.5</option> | |
<option>2</option> | |
<option>2.5</option> | |
<option selected="selected">3</option> | |
<option>3.5</option> | |
<option>4</option> | |
<option>4.5</option> | |
<option>5</option> | |
</select> | |
<label for="presenter2">Score for Soubhi</label> | |
<select name="soubhi" id="presenter2"> | |
<option>1</option> | |
<option>1.5</option> | |
<option>2</option> | |
<option>2.5</option> | |
<option selected="selected">3</option> | |
<option>3.5</option> | |
<option>4</option> | |
<option>4.5</option> | |
<option>5</option> | |
</select> | |
<input type="submit" value="Submit Scores" /> | |
</fieldset> | |
</form> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// vote from students | |
var express = require('express'); | |
var formidable = require('formidable'); | |
var mysql = require('mysql'); | |
var path = require("path"); | |
var util = require('util'); | |
var app = express(); | |
var pool = mysql.createPool({ | |
connectionLimit : 100, //important | |
host : 'localhost', | |
user : 'root', | |
password : '=[-p0o9i', | |
database : 'votes', | |
debug : false | |
}); | |
var Translate={};Translate.translate_map={" ":"_"}; | |
String.prototype.underscorize=function() //{return this.replace(/[^A-Za-z0-9\[\] ]/g,function(a){return Translate.translate_map[a]||a})}; | |
{ | |
var newstr = this.replace(/ /g,'_'); | |
return newstr.toLowerCase(); | |
} | |
function put_score_to_database(req,res,fields) { // actually putting score after confirmation | |
pool.getConnection(function(err,connection){ | |
if (err) { | |
res.json({"code" : 100, "status" : "Error in connection database"}); | |
return; | |
} | |
console.log('connected as id ' + connection.threadId); | |
presenter1=Object.keys(fields)[1]; | |
presenter2=Object.keys(fields)[2]; | |
name=fields['name'].underscorize(); | |
connection.query("UPDATE votes SET "+name+'='+fields[presenter1]+" WHERE speaker='"+presenter1+"';",function(err,rows){ | |
}); | |
connection.query("UPDATE votes SET "+name+'='+fields[presenter2]+" WHERE speaker='"+presenter2+"';",function(err,rows){ | |
connection.release(); | |
}); | |
connection.on('error', function(err) { | |
res.json({"code" : 100, "status" : "Error in connection database"}); | |
return; | |
}); | |
}); | |
} | |
// try adding score to database | |
function add_score_to_database(req,res,fields) { | |
var form = new formidable.IncomingForm(); | |
var user_id = req.param('id'); | |
form.parse(req, function (err, fields, files) { | |
pool.getConnection(function(err,connection){ | |
if (err) { | |
res.json({"code" : 100, "status" : "Error in connection database"}); | |
return; | |
} | |
console.log('connected as id ' + connection.threadId); | |
if (fields['name']== undefined){ | |
res.redirect('/'); | |
return; | |
} | |
presenter1=Object.keys(fields)[1]; | |
presenter2=Object.keys(fields)[2]; | |
name=fields['name'].underscorize(); | |
connection.query("select "+name+" from votes where speaker='"+presenter1+"';",function(err,rows){ | |
connection.release(); | |
if(!err) { | |
if (rows[0][name]!=null) { | |
res.redirect('/confirm?name='+fields['name']+'&presenter1='+presenter1+ | |
'&score1='+fields[presenter1]+'&presenter2='+presenter2+ | |
'&score2='+fields[presenter2]); | |
} | |
else { | |
put_score_to_database(req,res,fields); | |
res.end('Thank you for voting!'); | |
} | |
} | |
}); | |
connection.on('error', function(err) { | |
res.json({"code" : 100, "status" : "Error in connection database"}); | |
return; | |
}); | |
}); | |
}); | |
} | |
app.get("/",function(req,res){ | |
res.sendFile(path.join(__dirname+'/form.html')); | |
}); | |
app.post("/",function(req,res){ | |
// handle_database(req,res); | |
// processAllFieldsOfTheForm(req,res); | |
add_score_to_database(req,res,null); | |
}); | |
function getParameters(req){ | |
var name = req.param('name'); | |
var presenter1 = req.param('presenter1'); | |
var presenter2 = req.param('presenter2'); | |
var score1 = req.param('score1'); | |
var score2 = req.param('score2'); | |
if (name == undefined ||presenter1 == undefined | |
|| presenter2== undefined|| | |
score1 == undefined || score2 == undefined) | |
return null; | |
else | |
return {name:name,presenter1:presenter1, | |
presenter2:presenter2,score1:score1,score2:score2}; | |
} | |
app.get("/confirm",function(req,res){ | |
params=getParameters(req); | |
if (params==null) | |
res.redirect('/'); | |
res.sendFile(path.join(__dirname+'/confirm.html')); | |
}) | |
app.post("/confirm",function(req,res){ | |
var {name,presenter1,presenter2,score1,score2}=getParameters(req); | |
var form = new formidable.IncomingForm(); | |
form.parse(req, function (err, fields, files) { | |
myfield={name: name}; | |
myfield[presenter1]=score1; | |
myfield[presenter2]=score2; | |
if (fields.action=='Update') | |
{ | |
put_score_to_database(req,res,myfield); | |
res.end('Thank you for updating your votes!'); | |
return; | |
} | |
else | |
res.redirect("/"); | |
}); | |
}) | |
app.listen(3000); | |
// ---- not use --- | |
// example for handling mysql | |
function handle_database(req,res) { | |
pool.getConnection(function(err,connection){ | |
if (err) { | |
res.json({"code" : 100, "status" : "Error in connection database"}); | |
return; | |
} | |
console.log('connected as id ' + connection.threadId); | |
connection.query("select * from votes",function(err,rows){ | |
connection.release(); | |
if(!err) { | |
res.json(rows); | |
} | |
}); | |
connection.on('error', function(err) { | |
res.json({"code" : 100, "status" : "Error in connection database"}); | |
return; | |
}); | |
}); | |
} | |
// example for parse form | |
function processAllFieldsOfTheForm(req, res) { | |
var form = new formidable.IncomingForm(); | |
var user_id = req.param('id'); | |
form.parse(req, function (err, fields, files) { | |
//Store the data from the fields in your data store. | |
//The data store could be a file or database or any other store based | |
//on your application. | |
res.writeHead(200, { | |
'content-type': 'text/plain' | |
}); | |
res.write('received the data:\n\n'); | |
res.write(fields.name+'\n'); | |
res.end(util.inspect({ | |
fields: fields, | |
files: files, | |
id: user_id | |
})); | |
}); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment