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
принимал http-запрос ПОСЛЕ того, как sqlConn1 готов?
Вы можете listen
после подключения к 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.info('db connection failed');
}
}
init();