This repository has been archived by the owner on Apr 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.ts
112 lines (100 loc) · 2.99 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
'use strict'
import * as bodyParser from 'body-parser'
import * as history from 'connect-history-api-fallback'
import * as connectRedis from 'connect-redis'
import * as cookieParser from 'cookie-parser'
import * as express from 'express'
import * as mongoSanitize from 'express-mongo-sanitize'
import * as session from 'express-session'
import * as expressSimpleRoute from 'express-simple-route'
import * as logger from 'morgan'
import * as path from 'path'
import * as Config from './config'
import {redis} from './model'
import {haruhiMiddleware} from './module/middlewares'
const app = express()
app.set('trust proxy', ['loopback', 'uniquelocal'])
if (app.get('env') === 'development') {
app.use(logger('dev'))
} else {
app.use(logger('combined'))
}
const RedisStore = connectRedis(session)
app.use(cookieParser())
app.use(session({
store: new RedisStore({
client: redis as any,
prefix: 'haruhi:session:',
}),
...Config.session,
}))
// uncomment after placing your favicon in /public
// app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: false}))
app.use(mongoSanitize())
app.use(haruhiMiddleware)
/* eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports */
app.use('/', require('./route/index'))
expressSimpleRoute(path.join(__dirname, 'route'), app)
if (app.get('env') === 'development') {
app.use(history({
verbose: true,
}))
} else {
app.use(history())
}
/* eslint-disable @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports */
if (app.get('env') === 'development') {
const webpack = require('webpack')
const webpackConfig = require('./client/webpack.conf')
const compiler = webpack(webpackConfig)
const devMiddleware = require('webpack-dev-middleware')(compiler, {
publicPath: webpackConfig.output.publicPath,
stats : {
chunks: false,
colors: true,
},
})
const hotMiddleware = require('webpack-hot-middleware')(compiler)
app.use(devMiddleware)
app.use(hotMiddleware)
}
/* eslint-enable @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports */
app.use(express.static(path.join(__dirname, 'public')))
// catch 404 and forward to error handler
/* eslint-disable @typescript-eslint/no-unused-vars */
app.use((req, res, next): void => {
res.status(404)
.json({
message: 'not found',
})
})
// error handler
if (app.get('env') !== 'production') {
app.use((err, req, res, next): void => {
if (!err.status) {
req.logger.fatal({err})
err.status = 500
}
if (res.headersSent) return
res.status(err.status)
.json({
err,
message: err.message,
})
})
} else {
app.use((err, req, res, next): void => {
if (!err.status) {
req.logger.fatal({err})
err.status = 500
}
if (res.headersSent) return
res.status(err.status)
.json({
message: err.message,
})
})
}
module.exports = app