Your problem was primarily what I mentioned in my comment, so I will make it an answer. Don't hurt yourself slapping your forehead. :-) See my inline comments below. You had one method invocation too many, an unreturned result, and a misnamed variable - otherwise, it works just fine.
db_connect.js
console.log('db_connect module initialized');
var mysql = require('mysql');
function createConnection(){
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'officeball'
});
// ************ NOTE BELOW FOR CHANGE **************
// You didn't return anything from this function. You need to return the connection
return connection;
}
// ************ NOTE BELOW FOR CHANGE **************
// You are exporting a single connection by invoking createConnection();
// exports.createConnection = createConnection();
// what you want is:
exports.createConnection = createConnection;
exports.mysql = mysql;
validator.js
function validate(username, password, callback){
// ************ NOTE BELOW FOR CHANGE **************
// You had:
// var createConnection = connect.createConnection();
// but based on your code, you wanted to write this instead:
var connection = connect.createConnection();
/// ... REMAINDER OMITTED, because it was A-OK and this is already a long page
};
If you make those two three changes, you should be good to go. As ever, feel free to ask for any clarifications if it would be helpful.
Update This is how I am calling it - as you can see, I had it try to do it ever 2 seconds.
jt-test.js
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);
result
Connection with the Officeball MySQL database openned...
...Connection with the Officeball MySQL database closed.
success! { username: 'bagehot',
password: 'foo',
fname: 'walter',
lname: 'bagehot',
rank: 12 }
Connection with the Officeball MySQL database openned...
...Connection with the Officeball MySQL database closed.
success! { username: 'bagehot',
password: 'foo',
fname: 'walter',
lname: 'bagehot',
rank: 12 }
Connection with the Officeball MySQL database openned...
...Connection with the Officeball MySQL database closed.
success! { username: 'bagehot',
password: 'foo',
fname: 'walter',
lname: 'bagehot',
rank: 12 }
Connection with the Officeball MySQL database openned...
...Connection with the Officeball MySQL database closed.
success! { username: 'bagehot',
password: 'foo',
fname: 'walter',
lname: 'bagehot',
rank: 12 }
And so on. It runs indefinitely. Complete code for the three files is in this gist