-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
55 lines (48 loc) · 1.48 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
// server.js
import express from "express";
import path from "path";
import { fileURLToPath } from "url";
import cookieParser from "cookie-parser";
import "dotenv/config";
import { AccessToken } from "livekit-server-sdk";
const filename = fileURLToPath(import.meta.url);
const dirname = path.dirname(filename);
const createToken = async () => {
// if this room doesn't exist, it'll be automatically created when the first
// client joins
const roomName = "room1";
// identifier to be used for participant.
// it's available as LocalParticipant.identity with livekit-client SDK
const participantName = "User1";
const at = new AccessToken(
process.env.LIVEKIT_API_KEY,
process.env.LIVEKIT_API_SECRET,
{
identity: participantName,
// token to expire after 10 minutes
ttl: "10m",
}
);
at.addGrant({ roomJoin: true, room: roomName });
return await at.toJwt();
};
const app = express();
const port = process.env.PORT || 3000;
app.set("view engine", "ejs");
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.set("trust proxy", 1);
app.use(cookieParser());
app.use("/static", express.static("public"));
app.get("/", (req, res) => {
res.render(path.join(dirname, "public", "index"), {
wsServer: process.env.WS_SERVER,
});
});
app.get("/get-token", async (req, res) => {
const token = await createToken();
res.status(200).json(token);
});
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});