forked from masb0ymas/expresso-sequelize
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.ts
97 lines (81 loc) · 2.61 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import createError from 'http-errors'
import express, { Request, Response, NextFunction } from 'express'
import path from 'path'
import cors from 'cors'
import hpp from 'hpp'
import helmet from 'helmet'
import logger from 'morgan'
import requestIp from 'request-ip'
import bodyParser from 'body-parser'
import cookieParser from 'cookie-parser'
import userAgent from 'express-useragent'
import indexRouter from 'routes'
import withState from 'helpers/withState'
import ExpressErrorYup from 'middlewares/ExpressErrorYup'
import ExpressErrorResponse from 'middlewares/ExpressErrorResponse'
import ExpressErrorSequelize from 'middlewares/ExpressErrorSequelize'
import ExpressAutoHandleTransaction from 'middlewares/ExpressAutoHandleTransaction'
import winstonLogger, { winstonStream } from 'config/winston'
import initialJobs from 'jobs'
const GenerateDoc = require('utils/GenerateDocs')
const app = express()
// view engine setup
app.set('views', path.join(`${__dirname}/../`, 'views'))
app.set('view engine', 'pug')
app.use(helmet())
app.use(cors())
app.use(logger('combined', { stream: winstonStream }))
app.use(bodyParser.json({ limit: '100mb', type: 'application/json' }))
app.use(bodyParser.urlencoded({ extended: false }))
app.use(cookieParser())
app.use(express.static(path.join(`${__dirname}/../`, 'public')))
app.use(hpp())
app.use(userAgent.express())
app.use(requestIp.mw())
app.use((req: Request, res, next) => {
new withState(req)
next()
})
// Initial Docs Swagger
GenerateDoc(app)
// Initial Route
app.use(indexRouter)
// Initial Jobs
initialJobs()
async function handleRollbackTransaction(
err: any,
req: Request,
res: Response,
next: NextFunction
) {
try {
await req.rollbackTransactions()
// eslint-disable-next-line no-empty
} catch (e) {}
next(err)
}
app.use('/v1', handleRollbackTransaction)
app.use('/v1', ExpressErrorYup)
app.use('/v1', ExpressErrorSequelize)
app.use('/v1', ExpressErrorResponse)
app.use(ExpressAutoHandleTransaction)
// catch 404 and forward to error handler
app.use(function (req: Request, res: Response, next: NextFunction) {
next(createError(404))
})
// error handler
app.use(function (err: any, req: Request, res: Response, next: NextFunction) {
// set locals, only providing error in development
res.locals.message = err.message
res.locals.error = req.app.get('env') === 'development' ? err : {}
// add this line to include winston logging
winstonLogger.error(
`${err.status || 500} - ${err.message} - ${req.originalUrl} - ${
req.method
} - ${req.ip}`
)
// render the error page
res.status(err.status || 500)
res.render('error')
})
module.exports = app