-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
92 lines (67 loc) · 2.04 KB
/
Copy pathserver.js
File metadata and controls
92 lines (67 loc) · 2.04 KB
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
import express from 'express'
import dotenv from 'dotenv'
import path from 'path'
import fileupload from 'express-fileupload'
import cookieParser from 'cookie-parser'
import mongoSanitize from 'express-mongo-sanitize'
import helmet from 'helmet'
import xss from 'xss-clean'
import hpp from 'hpp'
import rateLimit from 'express-rate-limit'
import cors from 'cors'
import logger from './middleware/logger.js'
import morgan from 'morgan'
import errorHandler from './middleware/error.js'
import colors from 'colors'
import connectDB from './config/db.js'
//Load env vars
dotenv.config({ path: './config/config.env' })
// Connect to database
connectDB()
//Route files
import bootcamps from './routes/bootcampRoutes.js'
import courses from './routes/courseRoutes.js'
import auth from './routes/authRoutes.js'
import users from './routes/userRoutes.js'
import reviews from './routes/reviewRoutes.js'
const app = express()
// Body Parser
app.use(express.json())
// Cookie Parser
app.use(cookieParser())
// Dev logging middleware
if(process.env.NODE_ENV === 'development') {
app.use(morgan('dev'))
}
// File uploading
app.use(fileupload())
// Sanitize Data
app.use(mongoSanitize())
// Set security headers
app.use(helmet())
// Prevent XSS (Cross-SIte Scripting) attacks
app.use(xss())
// Rate Limiting
const limiter = rateLimit({
windowMs: 10 * 60 * 1000, // 10 Mins
max: 100
})
app.use(limiter)
// Prevent Http param pollution
app.use(hpp())
// Enable cors
app.use(cors())
// Set static folder
const __dirname = path.resolve()
app.use(express.static(path.join(__dirname, 'public')))
// Mount routers
app.use('/api/v1/bootcamps', bootcamps)
app.use('/api/v1/courses', courses)
app.use('/api/v1/auth', auth)
app.use('/api/v1/users', users)
app.use('/api/v1/reviews', reviews)
//Use error Middleware
app.use(errorHandler)
const PORT = process.env.PORT || 5000
app.listen(PORT,
console.log(`Server running in ${process.env.NODE_ENV} mode on port ${PORT}`.yellow.bold))