-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
40 lines (36 loc) · 1.7 KB
/
index.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
import { Express, RequestHandler, urlencoded } from 'express';
import cookieParser from 'cookie-parser';
import compression from 'compression';
import { helmetMiddleware } from '@/server/lib/middleware/helmet';
import { loggerMiddleware } from '@/server/lib/middleware/logger';
import { oktaDevMiddleware } from '@/server/lib/middleware/oktaDev';
import { csrfMiddleware } from '@/server/lib/middleware/csrf';
import { getConfiguration } from '@/server/lib/getConfiguration';
import { requestStateMiddleware } from '@/server/lib/middleware/requestState';
import { default as routes } from '@/server/routes';
import { routeErrorHandler } from '@/server/lib/middleware/errorHandler';
import { fourZeroFourMiddleware } from '@/server/lib/middleware/404';
import { requestIdMiddleware } from './requestId';
import { requestContextMiddleware } from './requestContext';
const { appSecret, stage } = getConfiguration();
export const applyMiddleware = (server: Express): void => {
// add request id middleware
server.use(requestIdMiddleware);
server.use(requestContextMiddleware);
// apply helmet before anything else
server.use(helmetMiddleware as RequestHandler);
server.use(urlencoded({ extended: true }) as RequestHandler);
server.use(cookieParser(appSecret));
server.use(compression());
// add the DEV okta middleware if state === DEV
if (stage === 'DEV') {
server.use(oktaDevMiddleware);
}
server.use(loggerMiddleware);
server.use(csrfMiddleware as RequestHandler);
// eslint-disable-next-line @typescript-eslint/no-misused-promises -- express has its own way of handling async middleware
server.use(requestStateMiddleware);
server.use(routes);
server.use(fourZeroFourMiddleware);
server.use(routeErrorHandler);
};