Update index.js for node tutorial

This commit is contained in:
pabloFuente 2024-05-10 12:47:27 +02:00
parent 12bd19854b
commit dca5cdc48e

View File

@ -1,6 +1,6 @@
import "dotenv/config";
import express from "express"; import express from "express";
import cors from "cors"; import cors from "cors";
import "dotenv/config";
import { AccessToken } from "livekit-server-sdk"; import { AccessToken } from "livekit-server-sdk";
const SERVER_PORT = process.env.SERVER_PORT || 6080; const SERVER_PORT = process.env.SERVER_PORT || 6080;
@ -13,22 +13,22 @@ app.use(cors());
app.use(express.json()); app.use(express.json());
app.post("/token", async (req, res) => { app.post("/token", async (req, res) => {
const roomName = req.body.roomName; const roomName = req.body.roomName;
const participantName = req.body.participantName; const participantName = req.body.participantName;
if (!roomName || !participantName) { if (!roomName || !participantName) {
res.status(400).json("roomName and participantName are required"); res.status(400).json("roomName and participantName are required");
return; return;
} }
const at = new AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET, { const at = new AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET, {
identity: participantName identity: participantName,
}); });
at.addGrant({ roomJoin: true, room: roomName }); at.addGrant({ roomJoin: true, room: roomName });
const token = await at.toJwt(); const token = await at.toJwt();
res.json(token); res.json(token);
}); });
app.listen(SERVER_PORT, () => { app.listen(SERVER_PORT, () => {
console.log("Server started on port:", SERVER_PORT); console.log("Server started on port:", SERVER_PORT);
}); });