你可以做类似的事情:
这里参考链接:1.stack https://stackoverflow.com/questions/45304238/how-can-i-use-two-database-connections-in-node-js-mysql 2.文件 https://www.npmjs.com/package/mysql#establishing-connections, 节点数据库连接 https://codeforgeek.com/2015/01/nodejs-mysql-tutorial/
var connection;
function createConnectionDB (host,dbName,userName,password) {
connection[dbName] = mysql.createConnection({
host : host,
user : userName,
password : password,
database : dbName
});
connection[dbName].connect();
};
// In API when You getting DBName 'db1' you can use
createConnectionDB('localhost','db1','username','password')
connection['db1'].query('SELECT * from < table name >', function(err, rows, fields) {
if (!err)
console.log('The solution is: ', rows);
else
console.log('Error while performing Query.');
});
connection['db1'].end();
////So API when You getting DBName ('db2') you can use
createConnectionDB('localhost','db2','username','password')
connection['db2'].query('SELECT * from < table name >', function(err, rows, fields) {
if (!err)
console.log('The solution is: ', rows);
else
console.log('Error while performing Query.');
});
connection['db2'].end();