-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
92 lines (63 loc) · 1.76 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
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
const express=require('express')
const dotenv=require('dotenv')
const fileupload=require('express-fileupload')
const path=require('path')
const cookieParser=require('cookie-parser')
const sanitize=require('express-mongo-sanitize')
const helmet=require('helmet')
const xss=require('xss-clean')
const rateLimit=require('express-rate-limit')
const hpp=require('hpp')
const cors=require('cors')
//Requirment packages
const bootcamp=require('./routes/bootcamp')
const course=require('./routes/course')
const user=require('./routes/user')
const auth=require('./routes/auth')
const review=require('./routes/review')
//routes
const logger=require('./middleware/logger')
const morgan=require('morgan')
const errorHandler=require('./middleware/error')
//Middlewares
const ConnectionDb=require('./config/db')
dotenv.config({path:'./config/config.env'})
const app=express()
const PORT=process.env.PORT
//server components
ConnectionDb()
app.use(express.json())
app.use(cookieParser())
if(process.env.NODE_ENV==='development'){
app.use(morgan('dev'))
}
app.use(fileupload())
app.use(sanitize())
app.use(helmet())
app.use(xss())
app.use(rateLimit({
windowMs:10*60*1000,
max:100,
message:{
result:"Too many request,please wait to some minutes"
}
}))
app.use(hpp())
app.use(cors())
app.use(express.static(path.join(__dirname,'public')))
app.use('/api/v1/bootcamps',bootcamp)
app.use('/api/v1/courses',course)
app.use('/api/v1/user',user)
app.use('/api/v1/auth',auth)
app.use('/api/v1/reviews',review)
//use the routes with middleware
app.use(errorHandler)
const server=app.listen(PORT,(error)=>{
if(error){
console.log(error)
}
})
process.on('unhandledRejection',(err,promise)=>{
console.log(err)
server.close(()=>process.exit(1))
})