-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
97 lines (81 loc) · 3.1 KB
/
server.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
/* This JavaScript code is setting up a Node.js server using Express framework. Here's a breakdown of
what each part of the code is doing: */
const connectDB = require("./env/db"); // Import the database connection function
const express = require("express"); // Import the Express framework
const morgan = require("morgan"); // Import Morgan, a logging middleware
const cors = require("cors"); // Import CORS middleware to handle Cross-Origin Resource Sharing
const cookieParser = require("cookie-parser"); // Import middleware to parse cookies
const fileupload = require("express-fileupload"); // Import middleware to handle file uploads
const errorHandler = require("./middlewares/error");
const dotenv = require("dotenv"); // Import dotenv to load environment variables from a .env file
const authRoute = require("./routers/auth-router");
const supportmailRoute = require("./routers/supportmail-router");
const http = require("http");
const socketIo = require("socket.io");
const mqttRoutes = require("./routers/mqttRoutes");
const {
subscribeToTopic,
getLatestLiveMessage,
} = require("./middlewares/mqttHandler");
// Load environment variables from .env file
dotenv.config({ path: "./.env" });
const app = express(); // Create an Express application
const server = http.createServer(app);
const io = socketIo(server);
// Middleware setup
app.use(express.json());
app.use(fileupload()); // Enable file uploads
app.use(express.urlencoded({ extended: false })); // Parse URL-encoded data
app.use(
cors({
// origin: ["http://localhost:3000"], // Allow only this origin
origin: "*", // Allow all the origin
methods: ["GET", "POST", "PUT", "DELETE", "PATCH"], // Allow these HTTP methods
})
);
app.use(cookieParser()); // Enable cookie parsing
app.use(morgan("dev"));
// Socket.IO connection
io.on("connection", (socket) => {
console.log("New client connected");
socket.on("subscribeToTopic", (topic) => {
if (!topic) {
socket.emit("error", { success: false, message: "Topic is required" });
return;
}
console.log(`Client subscribed to topic: ${topic}`);
const intervalId = setInterval(() => {
const latestMessage = getLatestLiveMessage(topic);
if (!latestMessage) {
socket.emit("noData", {
success: false,
message: "No live message available",
});
} else {
socket.emit("liveMessage", { success: true, message: latestMessage });
}
}, 1000);
socket.on("disconnect", () => {
console.log("Client disconnected");
clearInterval(intervalId);
});
socket.on("unsubscribeFromTopic", () => {
console.log(`Client unsubscribed from topic: ${topic}`);
clearInterval(intervalId);
});
});
});
//Routers
app.use("/api/v1/auth", authRoute);
app.use("/api/v1/supportmail", supportmailRoute);
app.use("/api/v1/mqtt", mqttRoutes);
app.set("socketio", io);
// Error handling middleware
app.use(errorHandler);
// Connect to the database
connectDB();
const port = process.env.PORT || 5000;
// Start the server on port 5000
server.listen(port, "0.0.0.0", () => {
console.log(`Listening on port number ${port}`);
});