-
Notifications
You must be signed in to change notification settings - Fork 24
/
app.js
43 lines (35 loc) · 1.07 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
require("dotenv").config();
const express = require("express");
const sequelize = require("./database"); // Update this line
const bodyParser = require("body-parser");
const app = express();
const PORT = process.env.PORT || 3000;
const taskRoutes = require("./routes/tasks");
app.use(bodyParser.json());
// Check DB connection
async function assertDatabaseConnectionOk() {
console.log(`Checking database connection...`);
try {
await sequelize.authenticate();
console.log("Database connection OK!");
} catch (error) {
console.log("Unable to connect to the database:");
console.log(error.message);
process.exit(1);
}
}
assertDatabaseConnectionOk();
// Define additional models and routes here
app.use("/", taskRoutes);
// Synchronize models with the database
sequelize
.sync({ alter: true, logging: false })
.then(() => {
console.log("Database & tables created!");
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});
})
.catch((error) => {
console.error("Failed to sync database:", error);
});