This repository has been archived by the owner on May 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
63 lines (50 loc) · 2.15 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
const express = require('express')
const cors = require('cors')
const axios = require('axios')
const path = require('path')
require('dotenv').config()
const theses = require('./data/meic_theses.json')
const areas = require('./data/meic_areas.json')
const app = express();
app.use(cors());
app.use(express.static(path.join(__dirname, 'client/build'))) //https://github.com/hemakshis/Basic-MERN-Stack-App/blob/master/app.js
app.get('/auth', async (req, res) => {
try {
var accessTokenResponse = await axios.post(
'https://fenix.tecnico.ulisboa.pt/oauth/access_token' +
`?client_id=${process.env.FENIX_CLIENT_ID}` +
`&client_secret=${encodeURIComponent(process.env.FENIX_CLIENT_SECRET)}` +
`&redirect_uri=${process.env.FENIX_REDIRECT_URI}` +
`&code=${encodeURIComponent(req.query.code)}` +
'&grant_type=authorization_code'
)
if (accessTokenResponse === undefined || accessTokenResponse.status != 200)
return console.log('accessTokenResponse is undefined or the status is not 200')
var personInformationResponse = await axios.get(
'https://fenix.tecnico.ulisboa.pt/api/fenix/v1/person' +
`?access_token=${accessTokenResponse.data.access_token}`
)
if (personInformationResponse === undefined || personInformationResponse.status != 200)
return console.log('personInformationResponse is undefined or the status is not 200')
} catch (error) {
return console.log(error)
}
res.send(personInformationResponse.data)
})
app.get('/theses/:area1?/:area2?', (req, res) => {
const area1 = req.params.area1
const area2 = req.params.area2
const checkedAreas = []
if (area1 !== undefined) checkedAreas.push(area1)
if (area2 !== undefined) checkedAreas.push(area2)
res.json(theses.filter(thesis => checkedAreas.every(area => thesis.areas.includes(area))))
})
app.get('/thesis/:id', (req, res) =>
res.json(theses.find(thesis => thesis.id === req.params.id))
)
app.get('/areas', (req, res) =>
res.json(areas)
)
app.listen(5000, () =>
console.log('App is running on port 5000.')
)