I am using node.js, mysql, ejs (templating engine) and bootstrap to create a website. I have tried to connect to mysql database by creating a connection (as seen in the code).
When i use nodemon, the first message that I get is that the server is connected on port 8089. However, the site does not connect to the sql database straight away. I have to restart nodemon (by pressing save) and then the site is connected to the database.
I am looking for a method which connects the site to the database on the first attempt when running nodemon.
const db = mysql.createConnection({
host: "localhost",
user: "root",
password: "root",
database: "MyFood"
});
db.connect((err) => {
if (err) {
// throw err;
console.error("error connecting: " + err.stack);
} else {
console.log("Connected to database");
}
});
global.db = db;