4 |
import Router from "./Router"; |
import Router from "./Router"; |
5 |
import path from "path"; |
import path from "path"; |
6 |
import rateLimit from 'express-rate-limit'; |
import rateLimit from 'express-rate-limit'; |
7 |
|
import cors from 'cors'; |
8 |
|
|
9 |
export interface ServerOptions { |
export interface ServerOptions { |
10 |
port?: number; |
port?: number; |
26 |
type methods = 'get' | 'post' | 'put' | 'patch' | 'delete'; |
type methods = 'get' | 'post' | 'put' | 'patch' | 'delete'; |
27 |
const expressRouter = ExpressRouter(); |
const expressRouter = ExpressRouter(); |
28 |
|
|
29 |
|
expressRouter.use(cors()); |
30 |
|
|
31 |
expressRouter.use(rateLimit({ |
expressRouter.use(rateLimit({ |
32 |
windowMs: 1 * 60 * 1000, |
windowMs: 1 * 60 * 1000, |
33 |
max: 50, |
max: 50, |
36 |
message: { code: 429, error: 'Too many requests at a time. Please try again later.' }, |
message: { code: 429, error: 'Too many requests at a time. Please try again later.' }, |
37 |
})); |
})); |
38 |
|
|
39 |
|
expressRouter.use(express.json()); |
40 |
|
expressRouter.use(express.urlencoded({ extended: true })); |
41 |
|
|
42 |
await this.router.loadRoutes(); |
await this.router.loadRoutes(); |
43 |
|
|
44 |
for (const route of this.router.routes) { |
for (const route of this.router.routes) { |
45 |
expressRouter[route.method.toLowerCase() as methods](route.path, ...route.middlewareList, await route.getCallbackFunction()); |
// console.log(route.callback[1], route.callback[0].middleware()[route.callback[1]]); |
46 |
|
expressRouter[route.method.toLowerCase() as methods](route.path, ...(route.callback[0].middleware()[route.callback[1]] as any[] ?? []), ...route.middlewareList as any[], await route.getCallbackFunction()); |
47 |
} |
} |
48 |
|
|
49 |
this.express.use(expressRouter); |
this.express.use(expressRouter); |