-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
72 lines (64 loc) · 2.2 KB
/
server.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
65
66
67
68
69
70
71
72
import 'dotenv/config';
import 'module-alias/register';
import 'reflect-metadata';
import * as bodyParser from 'body-parser';
import compression from 'compression';
import cookieParser from 'cookie-parser';
import cors from 'cors';
import express from 'express';
import Container from 'typedi';
import { ConnectionOptions, createConnection, useContainer } from 'typeorm';
import envConfig from './src/configs';
import { Routes } from './src/routes';
import { handleErrors } from './src/utils/handle-errors';
import { ValidateTokensMiddleware } from './src/utils/validate-tokens-middleware';
useContainer(Container);
class App {
public app: express.Application;
private routes: Routes = new Routes();
constructor() {
this.app = express();
this.config();
this.routes.routes(this.app);
this.app.use(handleErrors);
}
private config(): void {
const corsConfig = {
origin: envConfig.originUrl,
credentials: true,
};
this.app.use(cookieParser());
this.app.use(cors(corsConfig));
this.app.use(bodyParser.json());
this.app.use(bodyParser.urlencoded({ extended: true }));
this.app.disable('x-powered-by'); // disable X-Powered-By header
this.app.use(compression());
this.app.use(ValidateTokensMiddleware);
}
// tslint:disable-next-line: member-ordering
public async dbSetup(): Promise<void> {
const typeormconfig = this.getTypeOrmConfig();
// console.log(typeormconfig);
await createConnection(typeormconfig);
console.log('Connected to DB');
return;
}
private getTypeOrmConfig(): ConnectionOptions {
return {
type: 'mongodb',
host: envConfig.db.host,
port: parseInt(envConfig.db.port || '27017', 10),
database: envConfig.db.database,
username: envConfig.db.username,
password: envConfig.db.password,
entities: [__dirname + envConfig.db.entities],
logging: 'all', // envConfig.db.logging, // ['error' , 'query', 'all']
synchronize: envConfig.db.synchronize === 'true' ? true : false,
logger: 'advanced-console',
useNewUrlParser: true,
useUnifiedTopology: true,
dropSchema: envConfig.db.dropSchema === 'true' ? true : false,
};
}
}
export default App;