forked from ali-irawan/humanid-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
43 lines (35 loc) · 1.28 KB
/
server.js
File metadata and controls
43 lines (35 loc) · 1.28 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
'use strict'
const express = require('express'),
bodyParser = require('body-parser'),
WebConsoleController = require('./controllers/webconsole'),
MobileController = require('./controllers/mobile'),
WebController = require('./controllers/web')
class Server {
constructor(models, common, middlewares, nexmo) {
models = models
this.app = express()
this.app.use(bodyParser.json())
this.app.use(bodyParser.urlencoded({ extended: true }))
// routes
this.app.use('/', express.static('doc'))
this.app.use('/lib', express.static('client/dist'))
this.app.use('/examples', express.static('examples'))
this.app.use('/console', new WebConsoleController(models, common, middlewares).router)
this.app.use('/mobile', new MobileController(models, common, middlewares, nexmo).router)
this.app.use('/web', new WebController(models, common, nexmo).router)
// global error handler
this.app.use(function (err, req, res, next) {
console.error(err)
if (err.name === 'SequelizeValidationError' || err.name === 'ValidationError') {
return res.status(400).send(err.message)
} else {
return res.status(500).send(err.message)
}
})
// 404 handler
this.app.use(function(req, res){
res.status(404).send({ error: 'Unknown method' })
})
}
}
module.exports = Server