This is my first time working with OpenVidu and I’m trying to create video chat app using node. Below is my code but I’m getting error for god knows what reasons. I’m sending post request using postman and providing the body as per docs. I’m sharing the error at the end as well.
require("dotenv").config();
const express = require("express");
const bodyParser = require("body-parser");
const http = require("http");
const OpenVidu = require("openvidu-node-client").OpenVidu;
const cors = require("cors");
const app = express();
const session = require("express-session");
// Environment variables
const SERVER_PORT = process.env.SERVER_PORT || 5000;
const OPENVIDU_URL = process.env.OPENVIDU_URL || "[http://localhost:4443](http://localhost:4443/)";
const OPENVIDU_SECRET = process.env.OPENVIDU_SECRET || "MY_SECRET";
// Enable CORS support
app.use(cors());
// Use session middleware
app.use(
session({
saveUninitialized: true,
resave: false,
secret: "MY_SECRET",
})
);
const openvidu = new OpenVidu(OPENVIDU_URL, OPENVIDU_SECRET);
// In-memory user data (Replace this with database storage in production)
const users = [
{ username: "user1", password: "pass1" },
{ username: "user2", password: "pass2" },
];
// Allow application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));
// Allow application/json
app.use(bodyParser.json());
// Serve static resources if available
app.use(express.static(__dirname + "/public"));
// Serve application
const server = http.createServer(app);
server.listen(SERVER_PORT, () => {
console.log("Application started on port: ", SERVER_PORT);
console.warn("Application server connecting to OpenVidu at " + OPENVIDU_URL);
});
// Middleware to check if the user is authenticated
function isAuthenticated(req, res, next) {
if (req.session && req.session.isAuthenticated) {
return next();
}
res.status(401).send("Unauthorized");
}
// Login endpoint to authenticate the user
app.post("/api/login", (req, res) => {
const { username, password } = req.body;
const user = users.find(
(u) => u.username === username && u.password === password
);
if (user) {
// Mark the user as authenticated
req.session.isAuthenticated = true;
req.session.username = user.username;
res.send({ message: "Login successful" });
} else {
res.status(401).send({ error: "Invalid credentials" });
}
});
// Endpoint to create an OpenVidu session
app.post("/api/sessions", isAuthenticated, async (req, res) => {
try {
const session = await openvidu.createSession(req.body);
res.send({ sessionId: session.sessionId });
} catch (error) {
console.error("Error creating session:", error);
res.status(500).send("Error creating session");
}
});
app.post("/api/sessions/:sessionId/connections", async (req, res) => {
try {
var session = activeSessions.find(
(s) => s.sessionId === req.params.sessionId
);
if (!session) {
res.status(404).send();
} else {
var connection = await session.createConnection(req.body);
res.send(connection.token);
}
} catch (error) {
console.error("Error creating connection:", error);
res.status(500).send("Error creating connection");
}
});
// Add proper error handling for routes and middleware here
// For example:
app.use((err, req, res, next) => {
console.error(err);
res.status(500).send("Internal Server Error");
});
// Add global error handler for unhandled Promise rejections
process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled Promise rejection:', reason);
});
This is the error I’m getting
Error creating session: Error: [object Object]
at OpenVidu.handleError (C:\Users\Naaz\Desktop\Ali\test-openvid\node_modules\openvidu-node-client\lib\OpenVidu.js:694:20)
at C:\Users\Naaz\Desktop\Ali\test-openvid\node_modules\openvidu-node-client\lib\Session.js:513:26
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)