Connection to database. How to connect?
What is the best way to connect to database from backend?
Should we connect at the root of the app inside app.js
?
database.connect().then(response => {
console.log("Connected to database.");
app.listen(8080);
}).catch(error => {
console.log("Error connecting to database.");
})
Or, individual connection to each api call.
const getPosts = (res, req, next) => {
database.connect().then(response => {
console.log("Connected to database.");
console.log("Getting all posts.")
}).catch(error => {
console.log("Error connecting to database.");
})
}