const http = require('http');
var mysql2 = require('mysql2/promise');
var mysql2Conn;
const httpServer = http.createServer(function(req,res){
// error handler
// handlers
// req.on('data' ...
// req.on('end' ...
}).listen(config_private.localPort, async function (err){
// error handler
};
上面的代码应该放在哪里
mysql2Conn= await mysql2.createConnection({..params..});
让服务器.listen
并让http.createServer
的eventHandler在sqlConn1准备好后接受http请求?
最佳答案
连接到db
后,您可以监听
,例如:
const http = require('http');
var mysql2 = require('mysql2/promise');
var mysql2Conn;
const httpServer = http.createServer(function(req,res){
// error handler
// handlers
// req.on('data' ...
// req.on('end' ...
});
async function init() {
try {
mysql2Conn= await mysql2.createConnection({..params..});
httpServer.listen(config_private.localPort, async function (err){
// error handler
});
} catch(e) {
console.log('db connection failed');
}
}
init();
关于mysql - 如何在http服务器开始接受请求之前建立mysql2连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56325280/