In the following code, I am trying to retrieve data from MySQL and display them in HTML page. I tested the connection by showing the data in the console and it worked fine but the problem is that the data cannot be displayed on the HTML page:
var http = require("http");
var mysql = require('mysql');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
//response.end('Hello World\n');
var connection = mysql.createConnection(
{
host : 'localhost',
user : 'root',
password : 'somepass',
database : 'Students',
}
);
connection.connect();
var queryString = 'SELECT * FROM Student';
connection.query(queryString, function(err, rows, fields) {
if (err) throw err;
for (var i in rows) {
response.end('Name: ', rows[i].Name); //this doesn't work, it only shows Name: but doesn't show the retrieved name from the databased
console.log('Name: ', rows[i].Name); //this works fine
}
});
}).listen(8081);
console.log('Server running at http://127.0.0.1:8081/');
Can anybody help me solving this issue?