This repository has been archived by the owner on Jan 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.ts
64 lines (57 loc) · 1.78 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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 xss from "xss-clean";
import * as rateLimit from "express-rate-limit";
import * as mongoSanitize from "express-mongo-sanitize";
import * as hpp from "hpp";
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";
// Rate limiting
const limiter = rateLimit({
windowMs: 10 * 60 * 1000, // 10 mins
max: 1000, // No.of Requests
});
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());
this.express.use(xss());
this.express.use(mongoSanitize());
this.express.use(hpp());
this.express.use(limiter);
}
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;