This repository has been archived by the owner on Jan 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
50 lines (44 loc) · 1.37 KB
/
app.ts
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
import * as bodyParser from "body-parser";
import * as cors from "cors";
import * as express from "express";
import * as helmet from "helmet";
import * as morgan from "morgan";
import * as errorHandler from "./src/helpers/error.handler";
import apiV1 from "./src/api";
import config from "./src/config/config";
import { MongoHelper } from "./src/config/mongodb.config";
class App {
public express: express.Application;
constructor() {
this.express = express();
this.setMiddlewares();
this.setRoutes();
this.catchErrors();
this.connectToDB();
}
private setMiddlewares(): void {
this.express.use(cors());
this.express.use(morgan("dev"));
this.express.use(bodyParser.urlencoded({ extended: true }));
this.express.use(bodyParser.json());
this.express.use(helmet());
}
private setRoutes(): void {
this.express.use("/api/v1", apiV1);
}
private async connectToDB(): Promise<void> {
const MONGO_DB_URI = config.MONGO_DB_URI;
try {
await MongoHelper.connect(`${MONGO_DB_URI}`);
console.info(`Connected to MongoDB!`);
} catch (err) {
console.error(`Unable to connect to MongoDB!`, err);
}
}
private catchErrors(): void {
this.express.use(errorHandler.notFound);
this.express.use(errorHandler.internalServerError);
this.express.use(errorHandler.badRequest);
}
}
export default new App().express;