Connection.js
const mysql = require("mysql");
var mysqlConnection = mysql.createConnection({
host: "xxx.amazonaws.com",
user: "admin",
password: "xxx",
database: "xxx",
multipleStatements: true,
});
var connect= ()=> { return new Promise((resolve,reject)=>{
mysqlConnection.connect((err) => {
if (err) {
reject(err)
}
else{
resolve ("Success")
}
});
})
}
module.exports = { mysqlConnection,connect};Server.js
app.get("/", (req, res) => {
var fetchDetail = `xxxx`
connect()
.then((result)=>{
console.log(result)
mysqlConnection.query(fetchDetail, (error, result, fields) => {
if (error) {
console.log(error);
}
else {
console.log("Successfull");
res.send(result);
}
});
})
.catch((err)=>{
let e=new Error("Something went wrong")
next(e)
})
}
);
app.use(function (err, req, res, next) {
console.log(err)
res.status(500).send('Something broke!')
})如果我点击("/"),当我的互联网关闭时,我得到一个错误作为响应,因为服务器无法连接到Aws MySQL实例。但是,在我重新启动互联网并点击("/")之后,我仍然收到一个错误响应,直到我再次重新启动节点服务器。
目标:我不想在出现任何错误后重新启动服务器。
发布于 2020-08-11 18:55:19
您不需要重新启动所有服务器。您需要在.catch((err)=>{中的err上查找fatal标志,并尝试重新连接到mysql,然后通过再次调用mysql.createConnection({ ... params ... });来捕获致命错误(如断开连接)
https://stackoverflow.com/questions/63356646
复制相似问题