-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
117 lines (112 loc) · 3.17 KB
/
index.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//intranal import
const express = require("express");
const mongoose = require("mongoose");
const dotenv = require("dotenv");
//external import
const { notFoundHandler, errorHandler } = require("./middlewares/errorHandler");
const router = require("./controller/userController");
const converRouter = require("./controller/converSationControler");
const messageRouter = require("./controller/messageController");
const cookieParser = require("cookie-parser");
const path = require("path");
const cors = require("cors");
const jwt = require("jsonwebtoken");
const takaRouter = require("./controller/takaAdd");
const searchRouter = require("./controller/searchControler");
const http = require("http");
const { Server } = require("socket.io");
//init app
const app = express();
//socket server
const httpServer = http.createServer(app);
const io = new Server(httpServer, {
cors: ["https://hisabandchat.netlify.app", "http://localhost:5173"],
});
io.on("connection", (socket) => {
socket.on("join-room", function (data) {
socket.join(data);
});
socket.on("join-id", function (data) {
socket.join(data);
});
socket.on("send-message", function (datas) {
io.sockets.in(datas.conversation_id).emit("see-message", datas);
});
global.io = socket;
// .to(data.conversation_id)
// socket.on("join-id", (id) => {
// console.log(id);
//socket.join(id);
// });
socket.on("add", (bata) => {
io.sockets.in(bata.id).emit("get", bata);
});
socket.on("addOffice", function (data) {
io.sockets.in(data.id).emit("getOffice", data);
});
socket.on("dayUpdate", (data) => {
socket.emit("getDayUpdate", data);
});
socket.on("officeUpdate", (data) => {
socket.emit("getOfficeUpdate", data);
});
socket.on("dayDelete", (data) => {
socket.emit("getDayDelete", data);
});
socket.on("officeDelete", (data) => {
socket.emit("getOfficeDelete", data);
});
socket.on("tost", (data) => {
socket.emit("getTost", data);
});
socket.on("disconnect", () => {});
});
//dotenv config
dotenv.config();
//connsct connect
mongoose
.connect(process.env.CONNECTION_STRING)
.then(() => {
console.log("database connected");
})
.catch((err) => {
console.log(err);
});
//cors setup
app.use(
cors({
origin: [
"https://hisabandchat.netlify.app",
"https://65541ba9d4c7e03b57f28adc--hisabandchat.netlify.app/",
"https://65541e3d75121d3baa923995--hisabandchat.netlify.app",
"http://localhost:5173",
],
methods: "GET,PUT,POST,DELETE",
})
);
//data parse
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
//cookie parser
app.use(cookieParser(process.env.COOKIE_SECRET));
//set default folder
app.use("/images", express.static("public"));
//route handle;
app.get("/", (req, res) => {
res.status(200).json({
king: "iam the king",
});
});
app.use("/user", router);
app.use("/converSation", converRouter);
app.use("/message", messageRouter);
app.use("/takaAddAndGet", takaRouter);
app.use("/search", searchRouter);
//404 handle error
app.use(notFoundHandler);
//main error handler
app.use(errorHandler);
//app run
httpServer.listen(process.env.PORT, () => {
console.log(`app running port ${process.env.PORT}`);
});