gpt4 book ai didi

javascript - 错误: Connection lost: The server closed the connection. mysql Node

转载 作者:行者123 更新时间:2023-11-29 10:01:46 25 4
gpt4 key购买 nike

enter image description here

我在google上看到了很多这个问题的解决方案,但我无法应用它们。 API 运行良好,但一段时间后会出现此错误。

...................................................... ...................................................... ...................................................... ...................................................... ...................................................... …………

index.js

const express = require('express');
const app = express();
const router = express.Router();
const path = require('path');
const habalka = require('./routes/habalka')(router);

const port = process.env.PORT || 3000;

app.use('/api/habalka', habalka);

app.get('/', (req, res) => {
res.sendFile(path.join(__dirname + '/index.html'));
});

app.listen(port, () => console.log(`Example app listening on port ${port}!`));

db.js

 const mysql = require('mysql');

const db_config = mysql.createConnection({
host : '127.0.0.1',
user : 'root',
password : '',
database : 'habalka'
});

db_config.connect(function(err) {
if (err) {
console.log('error when connecting to db:', err);
}
});

module.exports = db_config;

哈巴尔卡.js

 const connect = require('../db');

module.exports = (router) => {

router.get('/get', (req, res) => {
let sql = 'SELECT * FROM test';
connect.query(sql, (err, results) => {
if (err) throw err;

res.json(results);
});
});

return router;
};

最佳答案

我建议使用 Sequelize ORM。它抽象了原始 SQL 的编写,并且更加安全。

关于javascript - 错误: Connection lost: The server closed the connection. mysql Node ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52797954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com