This repository has been archived by the owner on Jun 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.ts
85 lines (71 loc) · 1.82 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
require('string.prototype.matchall').shim()
require('array.prototype.flatmap').shim()
import './env'
import express from 'express'
import bodyParser from 'body-parser'
import busboy from 'connect-busboy'
import cookieParser from 'cookie-parser'
import cors from 'cors'
import morgan from 'morgan'
import * as routers from './routes'
import { logger } from './services'
import { calendar } from './utils'
const app: express.Application = express()
/* Middleware configuration */
const corsOptions = {
credentials: true,
origin:
process.env.NODE_ENV === 'development'
? 'http://localhost:3000'
: [
'https://upframe.io',
'https://connect.upframe.io',
'https://upfra.me',
'https://beta.upframe.io',
'https://dashboard.upframe.io',
],
}
app.use(cors(corsOptions))
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(cookieParser())
app.use(
busboy({
highWaterMark: 2 * 1024 * 1024, // Set 2MiB buffer
limits: {
fileSize: 5 * 1024 * 1024, // Max size of 5MB
},
})
)
/* Avoid empty POST requests */
app.use((req: express.Request, res: express.Response, next: any) => {
if (
Object.keys(req.body).length === 0 &&
req.method === 'POST' &&
req.path !== '/profile/image'
) {
const response = {
code: 400,
ok: 0,
message: 'Request body cannot be empty',
}
res.status(response.code).json(response)
return
}
next()
})
/* Logs configuration */
app.use(
morgan(
':remote-addr [:date[web]] :method :url :status :response-time ms - :res[content-length]'
)
)
app.set('logger', logger)
/* Routing */
routers.init(app)
calendar.init()
const port = process.env.PORT || 80
app.listen(port, () => {
logger.info('API started!')
logger.info('API listening on port ' + port)
})