-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
49 lines (44 loc) · 1.4 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
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const app = express()
const OpenApiValidator = require('express-openapi-validator').OpenApiValidator
const apicache = require('apicache')
apicache.options({appendKey: (req, res) => req.header('Accept-Language')})
const cache = apicache.middleware
const onlyStatus200 = (request, response) => response.statusCode === 200
const cacheSuccesses = cache('5 minutes', onlyStatus200)
// Parser for JSON
app.use(cacheSuccesses)
app.use(bodyParser.json())
app.use(bodyParser.text())
app.use(bodyParser.urlencoded({ extended: false }))
// Enable CORS
app.use(cors())
new OpenApiValidator({
apiSpec: 'util/OpenAPIContract.yaml',
validateRequests: true,
validateResponses: true
}).install(app).then(() => {
app.use((err, req, res, next) => {
res.status(err.status).json({
status: 400,
message: 'Bad Request',
errors: err.errors
})
})
// Inject routes
require('./routes/docs')(app)
require('./routes/collegeOrUniversity')(app)
require('./routes/studyProgram')(app)
require('./routes/module')(app)
require('./routes/videoLecture')(app)
require('./routes/vimeo')(app)
require('./routes/lecturers')(app)
require('./routes/lectureCodes')(app)
require('./routes/thumbnails')(app)
// Listen for REST-Calls
app.listen(3000, () => {
console.log('API started on port 3000...')
})
})