-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
58 lines (48 loc) · 1.98 KB
/
app.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import express from 'express'
import * as http from 'http'
import * as winston from 'winston'
import * as expressWinston from 'express-winston'
import cors from 'cors'
import { CommonRoutesConfig } from './common/common.routes.config'
import { UsersRoutes } from './users/users.routes.config'
import debug from 'debug'
const app: express.Application = express()
const server: http.Server = http.createServer(app)
const port = 3000
const routes: Array<CommonRoutesConfig> = []
const debugLog: debug.IDebugger = debug('app')
// here we are adding middleware to parse all incoming requests as JSON
app.use(express.json())
// here we are adding middleware to allow cross-origin requests
app.use(cors())
// here we are preparing the expressWinston logging middleware configuration,
// which will automatically log all HTTP requests handled by Express.js
const loggerOptions: expressWinston.LoggerOptions = {
transports: [new winston.transports.Console()],
format: winston.format.combine(
winston.format.json(),
winston.format.prettyPrint(),
winston.format.colorize({ all: true })
)
}
if (!process.env.DEBUG) {
loggerOptions.meta = false // when not debugging, log requests as one-liners
}
// initialize the logger with the above configuration
app.use(expressWinston.logger(loggerOptions))
// here we are adding the UserRoutes to our array,
// after sending the Express.js application object to have the routes added to our app!
routes.push(new UsersRoutes(app))
// this is a simple route to make sure everything is working properly
const runningMessage = `Server running at http://localshot:${port}`
app.get('/', (req: express.Request, res: express.Response) => {
res.status(200).send(runningMessage)
})
server.listen(port, () => {
routes.forEach((route: CommonRoutesConfig) => {
debugLog(`Routes configured for ${route.getName()}`)
})
// our only exception to avoiding console.log(), because we
// always want to know when the server is done starting up
console.log(runningMessage)
})