-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathapp.js
46 lines (36 loc) · 973 Bytes
/
app.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
'use strict';
var express = require('express');
var cors = require('express-cors')
var twilio = require('twilio')(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN);
var app = express();
app.use(cors({
allowedOrigins: [process.env.ALLOWED_ORIGIN],
methods: ['GET']
}));
var cachedToken = null;
function getNewToken () {
twilio.tokens.create({}, function(err, token) {
if (!err && token) {
cachedToken = token;
}
});
}
// fetch token initially
getNewToken();
// refetch new token every 15 mins and save to cache
setInterval(getNewToken, 1000*60*10);
app.get('/', function (req, res) {
if (!cachedToken) {
res.send(400, 'Problem getting ice servers data from Twilio')
} else {
res.json(cachedToken.iceServers);
}
});
var port = process.env.PORT || 3000;
app.listen(port, function (err) {
if (err) {
console.log('failed to start server', err);
} else {
console.log('started server on port:', port);
}
});