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..});
在sqlConn1准备好之后,让服务器进入.listen
并让http.createServer
的eventHandler接受http请求吗?
答案 0 :(得分:0)
连接到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.log('db connection failed');
}
}
init();