-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.ts
62 lines (56 loc) · 1.7 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
import express, { Application, NextFunction, Request, Response } from "express";
import { Controller } from "./interfaces/Controller";
import middlewareError from "./middlewares/ErrorMiddleware";
import AppError from "./utils/AppError";
import swaggerUI from "swagger-ui-express";
import * as swaggerDoc from "./apidocs.json";
import cors from "cors";
class AppStarter {
express: Application;
private port: string;
private options: object;
constructor(private controllers: Controller[], port: string) {
this.express = express();
this.port = port;
this.options = {
customSiteTitle: "Tummyfit",
};
this.express.disable("x-powered-by");
this.initMiddleware();
this.initControllers(controllers);
this.express.all(
"*",
(req: Request, response: Response, next: NextFunction) => {
next(
new AppError(`cant find ${req.originalUrl} on this server`, "404")
);
}
);
this.express.use(middlewareError);
}
private initMiddleware() {
this.express.use(cors());
this.express.use(express.json());
this.express.use(express.urlencoded({ extended: true }));
this.express.use("/images", express.static("uploads"));
this.express.use(
"/api-docs",
swaggerUI.serve,
swaggerUI.setup(swaggerDoc, this.options)
);
}
private initControllers(controllers: Controller[]) {
controllers.forEach((controller: Controller) => {
this.express.use("/api", controller.router);
});
}
public getApp() {
return this.express;
}
public listenServer() {
this.express.listen(this.port, () => {
console.log("Server is is listening to port " + this.port);
});
}
}
export default AppStarter;