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");
}
require
.for example
var sql = require('sql.js');
you need in the sql.js to return an object at the end with
module.exports = myobj
;Example:
You can create a module, and require it the following way.
File A: sql.js
Files B, C, D: