-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
56 lines (49 loc) · 1.4 KB
/
server.js
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
import 'dotenv/config'
import express, { json, urlencoded } from 'express'
import helmet from 'helmet'
import cors from 'cors'
import compression from 'compression'
import favicon from 'serve-favicon'
import responseTime from 'response-time'
import { join } from 'path'
import logger from './helpers/logger.js'
import { notFound, errorHandler } from './middlewares/errorHandler.js'
import { rateLimiter } from './middlewares/rateLimiterHandler.js'
import { speedLimiter } from './middlewares/speedLimiterHandler.js'
import { router } from 'express-file-routing'
const app = express()
app.use(responseTime())
app.use(helmet())
app.use(
cors({
origin: '*',
optionsSuccessStatus: 200
})
)
app.use(compression())
app.use(favicon(join(process.cwd(), 'public', 'favicon.ico')))
app.use(json())
app.use(
urlencoded({
extended: false
})
)
app.use('/api', rateLimiter, speedLimiter, await router())
app.use(notFound)
app.use(errorHandler)
const PORT = process.env.PORT || 5000
const HOST = process.env.HOST || 'localhost'
app.listen(PORT, () => {
logger.info(
`🚀 Server started at ${HOST} on PORT ${PORT} with processId: ${process.pid}`
)
})
process.on('unhandledRejection', (reason, promise) => {
logger.error('Unhandled Rejection at:', promise, 'reason:', reason)
process.exit(1)
})
process.on('uncaughtException', error => {
logger.error('Uncaught Exception thrown:', error)
process.exit(1)
})
export default app