-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathserver.js
75 lines (60 loc) · 1.91 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
require("dotenv").config();
const express = require("express");
const cors = require("cors");
const morgan = require("morgan");
const { default: fetch } = require("node-fetch");
const jwt = require("jsonwebtoken");
const PORT = 9000;
const app = express();
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(morgan("dev"));
//
app.get("/", (req, res) => {
res.send("Hello World!");
});
//
app.get("/get-token", (req, res) => {
const API_KEY = process.env.VIDEOSDK_API_KEY;
const SECRET_KEY = process.env.VIDEOSDK_SECRET_KEY;
const options = { expiresIn: "10m", algorithm: "HS256" };
const payload = {
apikey: API_KEY,
permissions: ["allow_join", "allow_mod"], // also accepts "ask_join"
};
const token = jwt.sign(payload, SECRET_KEY, options);
res.json({ token });
});
//
app.post("/create-meeting/", (req, res) => {
const { token, region } = req.body;
const url = `${process.env.VIDEOSDK_API_ENDPOINT}/api/meetings`;
const options = {
method: "POST",
headers: { Authorization: token, "Content-Type": "application/json" },
body: JSON.stringify({ region }),
};
fetch(url, options)
.then((response) => response.json())
.then((result) => res.json(result)) // result will contain meetingId
.catch((error) => console.error("error", error));
});
//
app.post("/validate-meeting/:meetingId", (req, res) => {
const token = req.body.token;
const meetingId = req.params.meetingId;
const url = `${process.env.VIDEOSDK_API_ENDPOINT}/api/meetings/${meetingId}`;
const options = {
method: "POST",
headers: { Authorization: token },
};
fetch(url, options)
.then((response) => response.json())
.then((result) => res.json(result)) // result will contain meetingId
.catch((error) => console.error("error", error));
});
//
app.listen(PORT, () => {
console.log(`API server listening at http://localhost:${PORT}`);
});