server.ts 1.19 KB
Newer Older
chinguyen's avatar
chinguyen committed
1 2 3 4 5 6 7 8
import * as express from 'express'
import * as winston from 'winston'
import * as boom from 'express-boom'
import * as morgan from 'morgan'
import * as cors from 'cors'
import { json, urlencoded } from 'body-parser'
import { Express } from 'express'
import * as routes from './routes/_index'
Trai Nguyen's avatar
Trai Nguyen committed
9 10 11
import * as specs from './swagger'
import * as swaggerUi from 'swagger-ui-express'

chinguyen's avatar
chinguyen committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25

const PORT: number = 3000

/**
 * Root class of your node server.
 * Can be used for basic configurations, for instance starting up the server or registering middleware.
 */
export class Server {
  private app: Express

  constructor() {
    this.app = express()

    // Express middleware
Trai Nguyen's avatar
Trai Nguyen committed
26 27 28
    this.app.use(cors({ optionsSuccessStatus: 200 }))
    this.app.use(urlencoded({ extended: true }))

chinguyen's avatar
chinguyen committed
29 30 31
    this.app.use(json())
    this.app.use(boom())
    this.app.use(morgan('combined'))
Trai Nguyen's avatar
Trai Nguyen committed
32 33 34 35 36

    if (process.env["EnvironmentName"] == "development") {
      this.app.use('/swagger', swaggerUi.serve, swaggerUi.setup(specs.default));
    }

chinguyen's avatar
chinguyen committed
37 38 39 40 41 42 43 44 45 46 47
    this.app.listen(PORT, () => {
      winston.log('info', '--> Server successfully started at port %d', PORT)
    })
    routes.initRoutes(this.app)
  }

  getApp() {
    return this.app
  }
}
new Server()