//连接数据库 var mysql = require('mysql'); var pool = mysql.createPool({ host: 'localhost', user: 'root', password: 'gfdfsdf34r34r', database:'caiji' }); module.exports = function(sql, callback) { pool.getConnection(function(conn_err, conn) { if(conn_err) { callback(err,null,null); } else { conn.query(sql, function(query_err, rows, fields) { conn.release(); callback(query_err, rows, fields); }); } }); };
调用
var query = require("./mysql.js"); query(sql, function(err, rows, fields) { console.log(rows); });