mysql2的callback与async写法
const express = require('express')
const mysql = require('mysql2')
const app = express()
app.listen(3001, () => {
console.log('\n\n\n\n\n\n listen to http://localhost:3001')
})
function getConfig () {
return {
host: 'localhost',
user: 'root',
database: 'test',
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0
}
}
const pool = mysql.createPool(getConfig())
const promisePool = pool.promise()
app.get('/async', async (req, res) => {
const results = await promisePool.query(`INSERT INTO konka(mac, boot_picture, boot_video) VALUES (?,?,?)`,
['54-BF-64-2B-04-31', '5.png', '5.mp4'])
res.send({
ok: 1,
data: results
})
})
app.get('/callback', (req, res) => {
pool.getConnection(function(err, conn) {
conn.query(
`INSERT INTO konka(mac, boot_picture, boot_video) VALUES (?,?,?)`,
['54-BF-64-2B-04-30', '4.png', '4.mp4'],
function(err, results) {
console.log(results);
res.send({
ok: 1,
data: results
})
})
pool.releaseConnection(conn);
})
})