-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
executable file
·60 lines (49 loc) · 1.34 KB
/
index.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
const fetch = require('node-fetch')
const ms = require('ms')
const chalk = require('chalk')
let data = []
const url = 'https://api.meetup.com/BigchainDB-IPDB-Meetup/events'
const log = text => console.log(text)
const logError = text => console.log(chalk.bold.red(text))
// Response handling for all fetch calls
const handleResponse = res => {
if (res.status !== 200) {
return logError('Non-200 response code from Meetup: ' + res.status)
}
return res.json()
}
//
// Fetch all upcoming meetups
//
const fetchMeetups = () => {
const start = Date.now()
fetch(url)
.then(res => {
return handleResponse(res)
})
.then(data_ => {
if (!data_) {
return
}
data = data_
log('Re-built meetups cache. ' +
`Total: ${data_.length} public meetups. ` +
`Elapsed: ${(new Date() - start)}ms`)
})
.catch(error => {
logError('Error parsing response from Meetup: ' + error.stack)
})
}
//
// Let's roll, and roll again every X ms
//
fetchMeetups()
setInterval(fetchMeetups, ms('15m'))
//
// Create the response
//
module.exports = async (req, res) => {
await res.setHeader('Access-Control-Allow-Origin', '*')
await res.setHeader('Access-Control-Allow-Methods', 'GET')
return data
}