-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.js
More file actions
47 lines (40 loc) · 1.19 KB
/
chat.js
File metadata and controls
47 lines (40 loc) · 1.19 KB
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
import express from "express";
import { Server } from "socket.io";
import { createServer } from "node:http";
import router from "./routes/homeRoute.js";
import cors from "cors";
import RegisterRouter from "./routes/userAuthRoute.js";
/* eslint-disable no-undef */
import dotenv from "dotenv";
dotenv.config();
const app = express();
const server = createServer(app);
const io = new Server(server, {
cors: {
origin: ["https://frontend-logic-chatapp.vercel.app"],
methods: ["GET", "POST"], // yeh likhna safe & recommended hai
},
serveClient: false,
});
app.use(cors());
io.on("connection", (socket) => {
console.log("a user connected");
socket.on("join-room", (key) => {
socket.join(key);
});
socket.on("send-message", ({ roomKey, message }) => {
io.to(roomKey).emit("receive-message", message);
});
});
app.get("/", (req, res) => {
console.log("✅ Hello from Vercel backend");
res.send("Hello World from backend!");
});
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use("/", router);
app.use("/chatJod", RegisterRouter);
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});