我正在使用ExpressJS導入MySQLJS。以ExpressJS作爲後端,我有一個名爲/get-candidates
的服務,ExpressJS試圖從MySQL表中獲取一些數據並返回給請求者。
我正在尋找一種在將JSON返回給請求者之前正確關閉MySQL DB連接的方法。ExpressJS - 如何正確關閉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)'
};