First install the package by running following command:
npm install mysql
Then in the code:
var mysql = require('mysql')
var connection = mysql.createConnection({
host: 'localhost',
user: 'dbuser',
password: 's3kreee7',
database: 'my_db'
});
// Open the connection
connection.connect();
// Now you can perform any query to MySQL database
connection.query('SELECT 1 + 1 AS solution', function (err, rows, fields) {
if (err) throw err
console.log('The solution is: ', rows[0].solution)
});
// Close the connection
connection.end();