forked from AgoraIO-Community/Agora-Node-TokenServer
-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.js
56 lines (49 loc) · 1.56 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
const express = require('express');
const {RtcTokenBuilder, RtcRole} = require('agora-access-token');
const PORT = 8080;
const APP_ID = process.env.APP_ID;
const APP_CERTIFICATE = process.env.APP_CERTIFICATE;
const app = express();
const nocache = (req, resp, next) => {
resp.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
resp.header('Expires', '-1');
resp.header('Pragma', 'no-cache');
next();
}
const generateAccessToken = (req, resp) => {
// set response header
resp.header('Acess-Control-Allow-Origin', '*');
// get channel name
const channelName = req.query.channelName;
if (!channelName) {
return resp.status(500).json({ 'error': 'channel is required' });
}
// get uid
let uid = req.query.uid;
if(!uid || uid == '') {
uid = 0;
}
// get role
let role = RtcRole.SUBSCRIBER;
if (req.query.role == 'publisher') {
role = RtcRole.PUBLISHER;
}
// get the expire time
let expireTime = req.query.expireTime;
if (!expireTime || expireTime == '') {
expireTime = 3600;
} else {
expireTime = parseInt(expireTime, 10);
}
// calculate privilege expire time
const currentTime = Math.floor(Date.now() / 1000);
const privilegeExpireTime = currentTime + expireTime;
// build the token
const token = RtcTokenBuilder.buildTokenWithUid(APP_ID, APP_CERTIFICATE, channelName, uid, role, privilegeExpireTime);
// return the token
return resp.json({ 'token': token });
}
app.get('/access_token', nocache, generateAccessToken);
app.listen(PORT, () => {
console.log(`Listening on port: ${PORT}`);
});