【发布时间】:2018-02-14 10:08:40
【问题描述】:
我正在使用ExpressJS 导入MySQLJS。使用 ExpressJS 作为后端,我有一个名为 /get-candidates 的服务,ExpressJS 尝试从 MySQL 表中获取一些数据并返回给请求者。
我正在寻找一种在将 JSON 返回给请求者之前正确关闭 MySQL 数据库连接的方法。
这是我的/get-candidates 的样子:
module.exports.getCandidates = function (request, response) {
var mysql = require("mysql");
var connectionSettings = require("../db.conf.json");
var connection = mysql.createConnection(connectionSettings);
connection.connect();
connection.query('SELECT * FROM Candidates', function (err, rows, fields) {
if (err) {
throw err;
} else {
response.json(rows);
}
});
connection.end(); // I don't think the code reaches this part after line 'response.json(rows)'
};
【问题讨论】:
标签: mysql node.js express promise