-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
76 lines (38 loc) · 1.18 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
64
65
66
67
68
69
70
71
72
73
74
75
76
const express = require('express');
const app = express();
const port = 3030;
const axios = require('axios');
const path = require('path');
const CircularJSON = require('circular-json');
const getEvents = (options = {}) => {
try {
console.log('http://open-api.myhelsinki.fi/v1/events/?limit=25' + options);
return axios.get('http://open-api.myhelsinki.fi/v1/events/' + options).then(response => CircularJSON.stringify(response.data))
}
catch (error) {
console.error('Axios error: ' + error)
}
}
app.use(function (req, res, next) {
res.setHeader("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.get('/api/events/', async (req, res, next) => {
console.log(req.query);
let options = '?'
for (let key in req.query) {
if (options != '?') {
options += '&'
}
options += key + '=' + encodeURIComponent(req.query[key]);
}
try {
const events = await getEvents(options)
res.json(JSON.parse(events));
}
catch (error) {
next(error)
}
});
app.listen(port, () => console.log('Server running in port: ' + port));