-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (51 loc) · 1.43 KB
/
index.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
44
45
46
47
48
49
50
51
52
53
54
55
56
require("dotenv").config();
const express = require("express");
const app = express();
const displayRoutes = require("express-routemap");
const sequelize = require("./config/db");
const messages = require("./constants");
const STATUS_CODES = require("./utils/statusCode");
const customerRoutes = require("./routes/customer_routes");
app.use(express.json());
app.use(customerRoutes);
app.get("/", (request, response) => {
response.status(STATUS_CODES.OK).json({
success: true,
message: messages.WELCOME_MESSAGE,
});
});
try {
(async () => {
await sequelize.authenticate();
// await sequelize.sync({ force: true });
console.log("Database connected successfully.");
app.listen(process.env.APP_PORT, () => {
displayRoutes(app);
console.log(`Example app listening on port ${process.env.APP_PORT}`);
});
})();
} catch (error) {
console.error("Unable to connect to the database:", error);
process.exit(1);
}
// error handling middleware
app.use((err, request, response, next) => {
if (err.sqlMessage || err.sqlState) {
return response.status(500).json({
status: "error",
message: messages.SOMETHING_WENT_WRONG,
});
} else {
return response.status(err.code || 400).json({
status: "error",
message: err.message,
});
}
});
// not found routes
app.use((request, response, next) => {
res.status(404).json({
status: "error",
message: "You got lost",
});
});