forked from w3tecch/express-typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpressLoader.ts
46 lines (40 loc) · 1.79 KB
/
expressLoader.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
import { Application } from 'express';
import { MicroframeworkLoader, MicroframeworkSettings } from 'microframework-w3tec';
import { createExpressServer } from 'routing-controllers';
import { authorizationChecker } from '../auth/authorizationChecker';
import { currentUserChecker } from '../auth/currentUserChecker';
import { env } from '../env';
export const expressLoader: MicroframeworkLoader = (settings: MicroframeworkSettings | undefined) => {
if (settings) {
const connection = settings.getData('connection');
/**
* We create a new express server instance.
* We could have also use useExpressServer here to attach controllers to an existing express instance.
*/
const expressApp: Application = createExpressServer({
cors: true,
classTransformer: true,
routePrefix: env.app.routePrefix,
defaultErrorHandler: false,
/**
* We can add options about how routing-controllers should configure itself.
* Here we specify what controllers should be registered in our express server.
*/
controllers: env.app.dirs.controllers,
middlewares: env.app.dirs.middlewares,
interceptors: env.app.dirs.interceptors,
/**
* Authorization features
*/
authorizationChecker: authorizationChecker(connection),
currentUserChecker: currentUserChecker(connection),
});
// Run application to listen on given port
if (!env.isTest) {
const server = expressApp.listen(env.app.port);
settings.setData('express_server', server);
}
// Here we can set the data for other loaders
settings.setData('express_app', expressApp);
}
};