-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.ts
More file actions
63 lines (48 loc) · 1.72 KB
/
app.ts
File metadata and controls
63 lines (48 loc) · 1.72 KB
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
import express, { Application } from 'express';
import cors from 'cors';
import bodyParser from 'body-parser';
import swaggerUi from "swagger-ui-express";
import IController from './controler/IController';
import { swaggerSpec } from './utils/swagger-docs';
import morgan from 'morgan';
import { Logger } from './utils/Loggeur';
class App {
public express: Application;
public port: number;
public logger: Logger;
public server: any;
constructor(controllers: IController[], port: number) {
this.express = express();
this.port = port;
this.logger = new Logger();
this.initialiseMiddleware();
this.initialiseControllers(controllers);
this.initialiseSwagger();
}
private initialiseMiddleware(): void {
this.express.use(cors());
this.express.use(morgan('dev'));
this.express.use(express.json());
this.express.use(express.urlencoded({ extended: false }));
this.express.use(bodyParser.json());
this.express.use(bodyParser.urlencoded({
extended: true
}));
}
private initialiseControllers(controllers: IController[]): void {
controllers.forEach((controller: IController) => {
this.express.use('/api', controller.router);
});
}
public listen(): void {
const server = this.express.listen(this.port, () => {
console.log(`⚡️[server] : App listening on the port ${this.port}`);
});
}
public initialiseSwagger(): void {
// Swagger page
this.express.use("/swagger", swaggerUi.serve, swaggerUi.setup(swaggerSpec),);
console.log(`Docs available at http://localhost:${this.port}/swagger`);
}
}
export default App;