TBH, feeling so noob... I know I'm making a few mistakes, but I've spent a considerable amount of time already on the connection between MySQL and Nodejs and I'm still unable to figure out the problem. A solution will be appreciated!
I'm using cPanel (with nodejs all setup), and have the following two files in the same directory:
index.html:
<!DOCTYPE html>
<html>
<body>
<h1>MySQL and Node.js</h1>
<script src="config_db.js"></script>
<script>
con.query('SELECT * FROM Index', (err,rows) => {
if(err) throw err;
document.write('Data received from Db:\n');
document.write(rows);
});
</script>
</body>
</html>
config_db.js
var mysql = require("mysql");
var con = mysql.createConnection({
host: 'localhost',
user: 'username_here',
password: 'password_here',
database: 'database_name_here'
});
con.connect(function(err){
if(err){
console.log('Error connecting to Db');
return;
}
console.log('Connection established');
});
In the output, I get nothing except for "MySQL and Node.js".