Node.js has a bug connecting MySQL with express.

  node.js, question

As a beginner node, I wrote a test script with express to connect to the database to query data. The code is as follows:

var express = require('express');
 var app = express();
 
 
 app.get('/hello', function (req, res) {
 var mysql      = require('mysql');
 var connection = mysql.createConnection({
 host     : '121.42.209.162',
 user     : 'root',
 Password:' Here is the password',
 database : 'hgb'
 });
 
 var obj;
 var isok='failed';
 connection.connect(function(err) {
 if (err) {
 Error ('errorconnecting:' plus err.stack');
 return;
 bracket
 Log ('connected asid' plus connection.threadId);
 });
 
 connection.query('SELECT * FROM `538`', function(err, rows) {
 if(err){
 console.log(err.code);  // 'ECONNREFUSED'
 console.log(err.fatal);  // true
 bracket
 isok='ok';
 });
 
 connection.end();
 
 res.send(isok);
 });
 
 var server = app.listen(3000, function () {
 var host = server.address().address;
 var port = server.address().port;
 
 Log ('exampleapplisteningatttp://percent s: percent s', host, port);
 });

But the browser openshttp://localhost:3000/helloAfter that, what is shown is
clipboard.png
At the same time, the log in xshell looks like this:
clipboard.png
Explain that the database connection was successful? !
However, if the query is successful, the callback function will be executed, which means isok will become’ ok’. . .
Please kindly answer, thank you. .

The program is executed asynchronously, and query is send before it is finished.