I'm new to javascript and need to know how to create a global function or class that can access by every other page on the website when it is required
mysqlConnection.js
function mysql.connection () {
const mysql = require('mysql');
const con = mysql.createConnection({
host: "hostname",
user: "username",
password: "password",
database: "database",
});
};
script.js
mysql = require('mysqlConnection.js');
mysql.connection();
con.connect(function (err) {
if (err) throw err;
// Query
con.query(sql, function (err, result) {
if (err) throw err;
// Result
});
});