I have mysql connection code which I need to call each time in every .js file. Say I want sql.js
from main.js
. I am thinking include(sql.js)
?
sql.js
var sql = require('sql');
var connection = sql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'db'
});
connection.connect(function(err){
if(!err) {
console.log("connected");
}
You can create a module, and require it the following way.
File A: sql.js
var a = function a(){
};
module.exports.a = a;
Files B, C, D:
var sql = require("./sql");
sql.a();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With