-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
56 lines (50 loc) · 1.44 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
import express from "express";
import dbConnect from "./utils/dbConnection.js";
import compression from "compression";
import cors from "cors";
import morgan from "morgan";
import cookieParser from "cookie-parser";
import ErrorMiddleware from "./middleware/errorMiddleware.js";
import helmet from "helmet";
import dotenv from "dotenv";
dotenv.config();
export class App {
constructor(controllers, port) {
this.express = express();
this.port = port;
this.initializeDatabaseConnection();
this.initializeMiddleware();
this.initializeControllers(controllers);
this.initializeErrorHandling();
}
initializeMiddleware = () => {
this.express.use(helmet());
this.express.use(cors(
{
origin:"http://localhost:3000",
credentials:true,
}
));
this.express.use(morgan("dev"));
this.express.use(express.json());
this.express.use(express.urlencoded({ extended: true }));
this.express.use(compression());
this.express.use(cookieParser());
};
initializeControllers = (controllers) => {
controllers.forEach((controller) => {
this.express.use(`/api/${controller?.subRoute}`, controller.router);
});
};
initializeErrorHandling = () => {
this.express.use(ErrorMiddleware);
};
initializeDatabaseConnection = async () => {
dbConnect();
};
listen() {
this.express.listen(this.port, () =>
console.log(`App listening on port ${this.port}`)
);
}
}