-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
84 lines (68 loc) · 2.19 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const express = require ('express');
const mysql = require ('mysql2');
const dotenv = require('dotenv');
const cors = require('cors');
const app = express();
const { initializeSequelize, db} = require('./models/index');
const {globalErrorHandler} = require('./middlewares/globalMiddleWare');
dotenv.config();
const PORT = process.env.PORT || 3000;
// Here I import the routes
const bookRoutes = require('./routes/books');
const authorRoutes = require('./routes/author');
const authRoute = require('./routes/auth')
const borrowRecordsRoute = require('./routes/borrowRecords');
const { generalLimiter } = require('./middlewares/rateLimit');
// Cors MiddleWare setup happens below
// Middleware to parse JSON
app.use(express.json());
app.use(cors({
origin:'*',
methods:['GET', 'POST', 'PUT', 'DELETE'],
allowedHeaders: ['Content-Type', 'Authorization'],
credentials: true,
preflightContinue: false
}))
// // Global Error Handler Middleware
// app.use((err,req,res,next)=>{
// console.log( err.stack );
// res.staus(err.status || 500 ).json({
// success: false,
// message: err.message
// })
// })
//Here I implent the routes middleware
app.use(generalLimiter);
app.use(borrowRecordsRoute);
app.use(bookRoutes);
app.use(authorRoutes);
app.use(authRoute);
// Here I Catch all the unhandled routes
app.all('*', (req, res, next) => {
next(new AppError(`Cannot find ${req.originalUrl} on this server!`, 404));
});
// Global error handling middleware
app.use(globalErrorHandler);
// Here I Sync the database and start the server
initializeSequelize()
.then((sequelize) => {
// Now I sync the Sequelize models (including Role)
return sequelize.sync();
})
.then(async () => {
// Check if roles exist and create them if they don't
const roles = ['Admin', 'Librarian', 'Member'];
for (const roleName of roles) {
const existingRole = await db.Role.findOne({ where: { name: roleName } });
if (!existingRole) {
await db.Role.create({ name: roleName });
}
}
console.log('Database synced!');
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
})
.catch((err) => {
console.error('Error syncing the database:', err);
});