我在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;
habalka.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;
};
答案 0 :(得分:1)
我建议使用Sequelize ORM。它抽象了编写原始SQL的过程,并且更加安全。