-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
147 lines (105 loc) · 3.14 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import express from "express";
import mongoose from "mongoose";
import "dotenv/config";
import userRouter from "./api/routes/user.route.js";
import auth from "./api/routes/auth.route.js";
import cors from "cors";
import cookieParser from "cookie-parser";
import postRouter from "./api/routes/post.route.js";
import messageRouter from "./api/routes/message.route.js";
import conversationRoute from "./api/routes/conversation.route.js";
import notificatonRoute from "./api/routes/notification.route.js";
import path from "path";
import http from "http";
import { Server } from "socket.io";
const app = express();
app.use(express.json());
app.use(cookieParser());
const expressServer = http.createServer(app);
//Handling CORS origin
if (process.env.NODE_ENV === "local") {
app.use(
cors({
origin: "http://localhost:5173",
credentials: true,
})
);
} else {
app.use(
cors({
origin:"*",
credentials: true,
methods:["GET","POST","PUT","PATCH","DELETE"]
})
);
}
const PORT = process.env.PORT || 3000;
// Connect to the database
main().catch((err) => console.log(err));
async function main() {
await mongoose.connect(process.env.MONGO);
console.log("Database connected");
}
// Starting the server
expressServer.listen(PORT, () => {
console.log(`Server running at port ${PORT}`);
});
// Routes
app.use("/api/users", userRouter);
app.use("/api/auth", auth);
app.use("/api/posts", postRouter);
app.use("/api/message", messageRouter);
app.use("/api/conversation", conversationRoute);
app.use("/api/notification", notificatonRoute);
//============== Deployment==============//
const __dirname = path.resolve();
if (process.env.NODE_ENV === "production") {
const staticFilesPath = path.join(__dirname, "client", "dist");
app.use(express.static(staticFilesPath));
app.get("*", (req, res) => {
res.sendFile(path.join(staticFilesPath, "index.html"));
});
} else {
app.get("/", (req, res) => {
res.send("api listing...");
});
}
//============== Deployment==============//
// Handle middleware
app.use((err, req, res, next) => {
const statusCode = err.statusCode || 500;
const message = err.message || "Internal Server Error";
return res.status(statusCode).json({
success: false,
statusCode,
message,
});
});
//----------------------------Handling Socket.io ------------------------------//
//Handling CORS origin
export const io = new Server(expressServer, {
cors: {
origin: [
"http://localhost:5173",
"https://property-sell.vercel.app",
"https://property-sell-gjz462ec1-emoncr.vercel.app/",
"http://localhost:3000",
"https://property-sell.onrender.com"
],
credentials: true,
},
});
io.on("connection", (socket) => {
console.log(`socket connected with ${socket.id}`);
//=======Messaging Feature Here ======//
socket.on("join_room", (chatId) => {
socket.join(chatId);
});
socket.on("send_message", (data) => {
socket.to(data.chatId).emit("receive_message", data);
socket.broadcast.emit(`${data.to}`, data);
});
socket.on("disconnect", (data) => {
console.log(`user disconnected successfully ${socket.id}`);
});
});