-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
98 lines (78 loc) · 2.08 KB
/
index.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
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
'use strict'
import express from 'express'
import connectDB from './config/db'
import helmet from 'helmet'
import cors from 'cors'
import morgan from 'morgan'
import { rateLimiter } from './middlewares'
const app = express()
const corsOptions = {
origin: process.env.CORS_ORIGIN,
methods: process.env.CORS_METHODS,
exposedHeaders: ['x-access-token'],
optionsSuccessStatus: 200
}
if (process.env.CORS_ORIGIN === '*') {
delete corsOptions.origin
}
app.use(morgan('tiny'))
app.use(cors(corsOptions))
app.use(helmet({
hsts: false
}))
app.use(helmet.hsts({
maxAge: 31536000,
includeSubDomains: true,
preload: true
}))
app.use(helmet.contentSecurityPolicy({
directives: {
defaultSrc: ['\'self\''],
styleSrc: ['\'self\'', '\'unsafe-inline\''],
scriptSrc: ['\'self\'', '\'unsafe-inline\'']
}
}))
app.use(express.json({ limit: '50mb' }))
app.use(express.urlencoded({ limit: '50mb', extended: true, parameterLimit: 50000 }))
// setup view
app.set('view engine', 'ejs')
app.use(express.static('public'))
// imoplement rate limitor on create shortlink API
app.use('/api/url/shorten', rateLimiter)
// connecting to db
connectDB()
// Health Check API
app.get('/health-check', (req, res, next) => {
res.status(200).json('ok')
})
// Define Routes
app.use('/', require('./routes/index'))
app.use('/api/url', require('./routes/shortUrl'))
app.use('*', (req, res, next) => {
const error = {
statusCode: 404,
message: ['Cannot', req.method, req.originalUrl].join(' ')
}
next(error)
})
app.use((error, req, res, next) => {
if (!error) {
return
}
const isParseError = error instanceof SyntaxError && error.status === 400
if (isParseError) {
return res.status(400).json('Invalid JSON body')
}
if (error.statusCode) {
if (error.statusCode === 404) {
return res.status(404).send('File not found')
}
return res.status(error.statusCode).json(error)
}
// console.log('[Error]', error)
return res.status(500).json(error)
})
const port = process.env.PORT || 5001
app.listen(port, () => {
// console.log(`server listening on port ${port}`)
})