-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
64 lines (53 loc) · 1.5 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
const express = require('express')
const app = express()
const path = require('path')
const logger = require('morgan')
const mongoose = require('mongoose')
const bodyParser = require('body-parser')
const cookieParser = require('cookie-parser')
const cors = require('cors')
// middlewares
app.use(express.static(path.join(__dirname, 'public')))
app.use('/public', express.static('public'))
app.use(logger('dev'))
require('dotenv/config')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(cookieParser())
// cors
const corsOption = {
origin: 'http://localhost:4200',
methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE']
}
app.use(cors(corsOption))
// server setup
app.listen(process.env.PORT, () => {
console.log('Sever is Running http://localhost' + process.env.PORT)
})
// database setup
mongoose.set('strictQuery', false)
mongoose.connect(process.env.CONNECTION_STRING).then(() => {
console.log('Database Connnected')
})
const userRouter = require('./routes/user.js')
const instructorRouter = require('./routes/instructor.js')
const adminRouter = require('./routes/admin.js')
// router middlewares
app.use('/', userRouter)
app.use('/admin', adminRouter)
app.use('/instructor', instructorRouter)
// error handling
app.use((req, res, next) => {
const error = new Error('Not found')
error.status = 404
next(error)
})
app.use((error, req, res, next) => {
res.status(error.status || 500)
res.json({
error: {
message: error.message
}
})
})
module.exports = app