Answer the question
In order to leave comments, you need to log in
How to transfer a database connection to a separate file?
Hello.
Tell me how to make a connection to the database in a separate file in nodejs?
//Db
var mysql= require('mysql');
//database.js
var connection=mysql.createConnection({host:'localhost', user:'root' ,password: '', database : 'nodejs'});
connection.connect(function(err){
if(!err) {
console.log("Database is connected ... nn");
}else {
console.log("Error connecting database ... nn");
}
});
exports.module=connection;
//file.js
var database= require('database');
database.connection.query('SELECT * FROM users', function(err, rows, fields) {
if (err) throw err;
console.log(rows);
});
database.connection.end();
//Как то так. Но такая схема не работает
Answer the question
In order to leave comments, you need to log in
mysqlconfig.js:
module.exports = exports = {
host:'localhost',
user:'root' ,
password: '',
database : 'nodejs'
};
var mysqlConfig = require('mysqlconfig.js');
var connection=mysql.createConnection(mysqlConfig);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question