-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
53 lines (41 loc) · 1.46 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
const express = require('express')
const dotenv = require('dotenv')
const bootcamps = require('./routes/bootcamp') //Rout files
const courses = require('./routes/courses')
const logger = require('./middleware/logger')
const morgan = require('morgan') //it is used for custom logging
const connectDB = require('./config/db')
const colors = require('colors')
const path = require('path')
const fileUpload = require('express-fileupload')
const auth = require('./routes/auth')
const errorHandler = require('./middleware/error')
const cookieParser = require('cookie-parser')
//Load Env Variables
dotenv.config({ path: './config/config.env'})
//connecting to the database
connectDB();
const app = express()
//using the body parser
app.use(express.json())
//Cookie Parser
app.use(cookieParser)
//our own custom logger
if(process.env.NODE_ENV === 'development')
{ app.use(morgan('dev')) }
//File uploading
app.use(fileUpload())
//set Static Folder
app.use(express.static(path.join(__dirname,'public')))
//Mount the Route
app.use('/api/v1/bootcamps',bootcamps)
app.use('/api/v1/courses',courses)
app.use('/api/v1/auth',auth)
app.use(errorHandler)
const PORT = process.env.PORT || 5000
const server=app.listen(PORT ,console.log(`Server running in ${process.env.NODE_ENV} mode and on ${process.env.PORT} port`.yellow.bold))
//handle unhandle rejections
process.on('unhandledRejection',(err,promise) => {
console.log(`Error : ${err.message}.red`)
server.close(() => process.exit(1))
})