-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
51 lines (43 loc) · 1.06 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
const express = require('express')
const app = express();
const morgan = require('morgan')
const cors = require('cors')
//db
require('./db')
//call routing level middleware
const apiRoute = require('./routes/api.route')
//load third-party middleware
app.use(morgan('dev'))
//accept every incoming request
app.use(cors())
//inbuilt middleware for parsing incoming data
app.use(express.urlencoded({
extended: true
}))
app.use(express.json()) //(for json)
app.get('/', function (req, res) {
res.json({
msg: "Hello"
})
});
//load routing level middleware(mount)
app.use('/api', apiRoute)
app.use(function (req, res, next) { //for undefined request
next({
msg: 'NOT FOUND 404',
status: 404
})
})
//error handling middleware
app.use(function (err, req, res, next) {
console.log('Error is >>', err)
res
.status(err.status || 400)
.json({
msg: err.msg || err,
status: err.status || 400
})
})
const server = require('http').createServer(app)
//socket stuff
require('./socket')(server)