OpenVidu / openvidu-tutorials

OpenVidu tutorials to get started
http://openvidu.io/tutorials
Apache License 2.0
228 stars 255 forks source link

Can not create session using nodejs #188

Closed AliYusufzai closed 1 year ago

AliYusufzai commented 1 year ago

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"; 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)

`

CSantosM commented 1 year ago

Here is an official backend sample using NodeJS. https://github.com/OpenVidu/openvidu-tutorials/tree/master/openvidu-basic-node

AliYusufzai commented 1 year ago

Yes, I tried the above code as well. same error

CSantosM commented 1 year ago

After checking it everything is working as expected. Here is the doc guide for getting started with openvidu-js https://docs.openvidu.io/en/2.28.0/tutorials/openvidu-js/#openvidu-js

AliYusufzai commented 1 year ago

See for yourself, I've given it the body in accordance to the docs yet it crashes the api with an error "Error[object,Object"]. https://ibb.co/HgKStcX