Last active
January 19, 2016 11:10
-
-
Save barry-johnson/9485669 to your computer and use it in GitHub Desktop.
Related to answer for: http://stackoverflow.com/questions/22238744/node-mysql-reusable-connection-module/22318071
This file contains 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
console.log('db_connect module initialized'); | |
var mysql = require('mysql'); | |
function createConnection(){ | |
var connection = mysql.createConnection({ | |
host : 'localhost', | |
user : 'xyz', | |
password : 'xyz', | |
database : 'xyz', | |
debug : false | |
}); | |
return connection; | |
} | |
exports.createConnection = createConnection; | |
exports.mysql = mysql; |
This file contains 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
var v = require('./validator'); | |
var timers = require('timers'); | |
var connections = 0; | |
timers.setInterval(function(){ | |
v.validate('bagehot','foo',function(err,result){ | |
if (err) | |
console.log('failed', err); | |
else | |
console.log('success! ',result); | |
}); | |
},2000); |
This file contains 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
console.log('server module initialized'); | |
var express = require('express'); | |
var login = require("./login"); | |
var validator = require("./validator"); | |
var app = express(); | |
function loginFail(req,res,mext){ | |
res.end('failed'); | |
}; | |
function loginSucceed(req,res,next){ | |
res.end('OK'); | |
} | |
function listen() { | |
app.use(express.bodyParser()); | |
app.get('/login', function(req, res) { | |
var username = req.query.username; | |
var password = req.query.password; | |
console.log('User ' + username + ' is attempting login...'); | |
validator.validate(username,password,function(err,result){ | |
if (err) loginFail(req,res,err); | |
else loginSucceed(req,res,result); | |
}); | |
}); | |
app.listen(8080, function() { | |
console.log('Server running at http://127.0.0.1:8080/'); | |
}); | |
} | |
listen(); |
This file contains 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
console.log('validator module initialized'); | |
var connect = require("./db_connect"); | |
exports.validate = validate; | |
function validate(username, password, callback){ | |
var connection = connect.createConnection(); | |
//the idea is for this to return the object, connection, | |
//which opens a new connection | |
connection.connect(function (err){ | |
if (err) return callback(new Error('Failed to connect'), null); | |
console.log('Connection with the Officeball MySQL database openned...'); | |
connection.query("select 'bagehot' as username,'foo' as password,'walter' as fname, 'bagehot' as lname,12 as rank, 'yes' as active from dual", | |
username, | |
function(err,rows,fields) { | |
connection.destroy(); | |
console.log('...Connection with the Officeball MySQL database closed.'); | |
if (err) | |
return callback(new Error ('Error while performing query'), null); | |
if (rows.length !== 1) | |
return callback(new Error ('- [Anomaly] - Failed to find exactly one user'), null); | |
if (rows[0].password === password & rows[0].active === "yes") { | |
var result = new Object; | |
result.username = rows[0].username; | |
result.password = rows[0].password; | |
result.fname = rows[0].fname; | |
result.lname = rows[0].lname; | |
result.rank = rows[0].rank; | |
return callback(null, result); | |
} if(rows[0].active !== "yes"){ | |
return callback(new Error ('User account not active.'), null); | |
}else { | |
return callback(new Error ('Login credentials did not match.'), null); | |
} | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment