Last active
October 9, 2022 07:43
-
-
Save barisbll/09619ba274f2cf915fa2c1f59f2de4bc to your computer and use it in GitHub Desktop.
Server class that returns express app as a singleton
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import compression from 'compression'; | |
import cors from 'cors'; | |
import express, { Express } from 'express'; | |
import 'reflect-metadata'; | |
import { createRouter as routes } from '../api/rest/v1/routes/routes'; | |
import logger from '../config/logger'; | |
import morganMiddleware from '../config/morgan'; | |
export class Server { | |
private static app: Express; | |
// eslint-disable-next-line no-useless-constructor, no-empty-function | |
private constructor() {} | |
public static getServer(): Express { | |
if (!Server.app) { | |
const app = express(); | |
Server.app = app; | |
try { | |
Server.app.use(express.json()); | |
Server.app.use(cors()); | |
Server.app.use(compression()); | |
Server.app.use(morganMiddleware); | |
Server.app.use('/api/v1', routes()); | |
return Server.app; | |
} catch (error) { | |
logger.error(error); | |
throw error; | |
} | |
} | |
return Server.app; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment