I have managed to fetch some information from my MySQL database with the code that follows this paragraph. I was wondering if anyone could help me get the MySQL information that is written onto the console by the code listed below (into a div) on the client.html file.
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
, mysql = require('mysql')
, connection = mysql.createConnection({
host : 'localhost',
port : '8889',
user : 'root',
password : 'root',
database : 'dbname'
}),
POLLING_INTERVAL = 3000,
pollingTimer;
// If there is an error connecting to the database
connection.connect(function(err) {
// connected! (unless `err` is set)
console.log( err );
});
// creating the server
app.listen(9999);
// on server started we can load our client.html page
function handler ( req, res ) {
fs.readFile( __dirname + '/client.html' ,
function ( err, data ) {
if ( err ) {
console.log( err );
res.writeHead(500);
return res.end( 'Error loading client.html' );
}
res.writeHead( 200 );
res.end( data );
});
};
var queryString = 'SELECT * FROM Switches';
connection.query(queryString, function(err, rows, fields) {
if (err) throw err;
for (var i in rows) {
console.log('Pin #: ' , rows[i].DBROW1);
console.log('Status: ', rows[i].DBROW2);
}
});
connection.end();