const express = require('express');
const bodyParser = require('body-parser');
const twilio = require('twilio');
const AccessToken = require("twilio").jwt.AccessToken;
require("dotenv").config();
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
const PORT = process.env.PORT || 3001;
const ACCOUNT_SID = process.env.ACCOUNT_SID;
const API_KEY_SID = process.env.API_KEY_SID;
const API_KEY_SECRET = process.env.API_KEY_SECRET;
//Audio
const OUTGOING_APP_SID = process.env.OUTGOING_APP_SID;
app.all('/', async (req, res) => {
return res.json({
status: true,
message: 'Hello from a Node JS Server'
});
});
app.all('/api/voice-token/:userId', async (req, res) => {
const VoiceGrant = AccessToken.VoiceGrant;
const uniqueIdentity = req.params.userId;
const grant = new VoiceGrant({
outgoingApplicationSid: OUTGOING_APP_SID,
incomingAllow: true,
});
const accessToken = new AccessToken(
ACCOUNT_SID,
API_KEY_SID,
API_KEY_SECRET,
{ identity: uniqueIdentity }
);
accessToken.addGrant(grant);
//These lines for CORS support
res.set("Access-Control-Allow-Origin", "*");
res.set("Access-Control-Allow-Methods", "GET");
res.set("Access-Control-Allow-Headers", "Content-Type");
res.set("Content-Type", "application/json");
return res
.status(200)
.json({ type: "voice", identity: uniqueIdentity, token: accessToken.toJwt() });
});
app.all('/api/video-token/:userId', async (req, res) => {
const VideoGrant = AccessToken.VideoGrant;
const uniqueIdentity = req.params.userId;
const grant = new VideoGrant({
room: 'cool room',
});
const accessToken = new AccessToken(
ACCOUNT_SID,
API_KEY_SID,
API_KEY_SECRET,
{ identity: uniqueIdentity }
);
accessToken.addGrant(grant);
//These lines for CORS support
res.set("Access-Control-Allow-Origin", "*");
res.set("Access-Control-Allow-Methods", "GET");
res.set("Access-Control-Allow-Headers", "Content-Type");
res.set("Content-Type", "application/json");
return res
.status(200)
.json({ type: "video", identity: uniqueIdentity, token: accessToken.toJwt() });
});
app.listen(PORT, () => {
console.log(`Server running on PORT ${PORT}`);
});
0 Comments
Post a Comment