UNPKG

typescript-express-starter

Version:
98 lines (86 loc) 2.78 kB
import 'reflect-metadata'; import { MikroORM, RequestContext } from '@mikro-orm/core'; import express from 'express'; import cookieParser from 'cookie-parser'; import compression from 'compression'; import cors from 'cors'; import helmet from 'helmet'; import hpp from 'hpp'; import morgan from 'morgan'; import swaggerJSDoc from 'swagger-jsdoc'; import swaggerUi from 'swagger-ui-express'; import { NODE_ENV, PORT, LOG_FORMAT, ORIGIN, CREDENTIALS } from '@config'; import { DI, dbOptions } from '@database'; import { UserEntity } from '@entities/users.entity'; import { Routes } from '@interfaces/routes.interface'; import { ErrorMiddleware } from '@middlewares/error.middleware'; import { logger, stream } from '@utils/logger'; export class App { public app: express.Application; public env: string; public port: string | number; constructor(routes: Routes[]) { this.app = express(); this.env = NODE_ENV || 'development'; this.port = PORT || 3000; this.connectToDatabase(); this.initializeMiddlewares(); this.initializeRoutes(routes); this.initializeSwagger(); this.initializeErrorHandling(); } public listen() { this.app.listen(this.port, () => { logger.info(`=================================`); logger.info(`======= ENV: ${this.env} =======`); logger.info(`🚀 App listening on the port ${this.port}`); logger.info(`=================================`); }); } public getServer() { return this.app; } private async connectToDatabase() { try { DI.orm = await MikroORM.init(dbOptions); DI.em = DI.orm.em; DI.userRepository = DI.orm.em.getRepository(UserEntity); } catch (error) { logger.error(error); } finally { this.app.use((_1, _2, next) => RequestContext.create(DI.orm.em, next)); } } private initializeMiddlewares() { this.app.use(morgan(LOG_FORMAT, { stream })); this.app.use(cors({ origin: ORIGIN, credentials: CREDENTIALS })); this.app.use(hpp()); this.app.use(helmet()); this.app.use(compression()); this.app.use(express.json()); this.app.use(express.urlencoded({ extended: true })); this.app.use(cookieParser()); } private initializeRoutes(routes: Routes[]) { routes.forEach(route => { this.app.use('/', route.router); }); } private initializeSwagger() { const options = { swaggerDefinition: { info: { title: 'REST API', version: '1.0.0', description: 'Example docs', }, }, apis: ['swagger.yaml'], }; const specs = swaggerJSDoc(options); this.app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(specs)); } private initializeErrorHandling() { this.app.use(ErrorMiddleware); } }