const express = require('express')
const bodyParser = require('body-parser')
const mysql = require('mysql')
const app = express()
app.use(bodyParser.json())
var connention = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'password',
database: 'mydatabase',
connectTimeout: 10000,
})
const sql = `select * from dddd limit 10`;
let sqlList = ''
connention.connect((err) => {
if (err) {
console.log(err);
} else {
console.log('已连接到数据库');
console.log('连接状态:', connention.state);
connention.query(sql, (err, result) => {
if (err) {
console.log(err);
} else {
console.log('查询数据表成功', connention.state);
sqlList = result
}
});
}
});
connection.on('end', () => {
console.log('Connection closed');
});
connection.on('error', (err) => {
console.error('Connection error:', err);
});
app.listen(8001,()=>{
console.log('Server is running on port 8001');
})