-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
70 lines (58 loc) · 1.87 KB
/
Copy pathserver.js
File metadata and controls
70 lines (58 loc) · 1.87 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const express = require('express')
const app = express()
const port = process.env.PORT || 3000
const studenthomeRoutes = require('./src/routes/studenthome-routes')
const mealRoutes = require('./src/routes/meal-routes')
const authenticationRoutes = require('./src/routes/authentication-routes')
const logger = require('tracer').console()
app.use(express.json()) // for parsing application/json
// Add CORS headers
app.use( (req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader(
'Access-Control-Allow-Methods',
'GET, POST, OPTIONS, PUT, PATCH, DELETE'
)
res.setHeader(
'Access-Control-Allow-Headers',
'X-Requested-With,content-type,authorization'
)
res.setHeader('Access-Control-Allow-Credentials', true)
next()
})
// logger
app.all("*", (req, res, next) => {
logger.log("Endpoint called: " + req.method + " " + req.url)
next()
})
// Install the routes
app.use('/api/studenthome', studenthomeRoutes)
app.use('/api/studenthome', mealRoutes)
app.use('/api', authenticationRoutes)
// UC-103 Systeeminfo opvragen
app.get('/api/info', (req, res) => {
const info = {
name: 'Justin Rodrigues da Silva',
studentNr: 2144403,
description: 'This is a api that returns information about studenthomes and meals',
sonarQubeUrl:'https://sonarqube.avans-informatica-breda.nl/dashboard?id=programmeren-4-samen-eten'
}
res.status(200).json(info)
})
// Catch all endpoint
app.all("*", (req, res, next) => {
logger.log("catch-all endpoint called")
next({message: 'Endpoint does not exist', errorCode: 401})
})
// Error handler
app.use("*", (error, req, res, next) => {
logger.log("Errorhandler called!", error)
res.status(error.errorCode).json({
error: "Some error occured",
message: error.message
})
})
app.listen(port, () => {
logger.log(`Example app listening at http://localhost:${port}`)
})
module.exports = app