-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathapp.js
110 lines (85 loc) · 2.73 KB
/
app.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
99
100
101
102
103
104
105
106
107
108
109
110
const express = require('express')
const path = require('path')
const dotenv = require('dotenv')
const colors = require('colors')
const morgan = require('morgan')
const cookieParser = require('cookie-parser')
const mongoSanitize = require('express-mongo-sanitize')
const fileupload = require('express-fileupload')
const helmet = require('helmet')
const xss = require('xss-clean')
const rateLimit = require('express-rate-limit')
const hpp = require('hpp')
const cors = require('cors')
const errorHandler = require('./middleware/error')
const DBConnection = require('./config/db')
dotenv.config({ path: './config/.env' })
DBConnection()
const authRoutes = require('./routes/auth')
const userRoutes = require('./routes/users')
const categoryRoutes = require('./routes/categories')
const videoRoutes = require('./routes/videos')
const commentRoutes = require('./routes/comments')
const replyRoutes = require('./routes/replies')
const feelingRoutes = require('./routes/feelings')
const subscriptionRoutes = require('./routes/subscriptions')
const historiesRoutes = require('./routes/histories')
const searchRoutes = require('./routes/search')
const app = express()
app.use(express.json())
app.use(cookieParser())
if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev'))
}
// File uploading
app.use(
fileupload({
createParentPath: true,
})
)
// Sanitize data
app.use(mongoSanitize())
// Set security headers
app.use(helmet())
// Prevent XSS attacks
app.use(xss())
// Enable CORS
app.use(cors())
// Rate limiting
// const limiter = rateLimit({
// windowMs: 10 * 60 * 1000, // 10 mins
// max: 100 // 100 request per 10 mins
// })
// app.use(limiter)
// Prevent http param pollution
app.use(hpp())
app.use(express.static(path.join(__dirname, 'public')))
// app.use((req, res, next) => {
// setTimeout(() => {
// next()
// }, 1000)
// })
const versionOne = (routeName) => `/api/v1/${routeName}`
app.use(versionOne('auth'), authRoutes)
app.use(versionOne('users'), userRoutes)
app.use(versionOne('categories'), categoryRoutes)
app.use(versionOne('videos'), videoRoutes)
app.use(versionOne('comments'), commentRoutes)
app.use(versionOne('replies'), replyRoutes)
app.use(versionOne('feelings'), feelingRoutes)
app.use(versionOne('subscriptions'), subscriptionRoutes)
app.use(versionOne('histories'), historiesRoutes)
app.use(versionOne('search'), searchRoutes)
app.use(errorHandler)
const PORT = process.env.PORT
const server = app.listen(PORT, () => {
console.log(
`We are live on ${process.env.NODE_ENV} mode on port ${PORT}`.yellow.bold
)
})
// Handle unhandled promise rejections
process.on('unhandledRejection', (err, promise) => {
console.log(`Error: ${err.message}`.red)
// Close server & exit process
server.close(() => process.exit(1))
})