-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
72 lines (61 loc) · 1.92 KB
/
Copy pathserver.js
File metadata and controls
72 lines (61 loc) · 1.92 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
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
const express = require("express");
const mongoose = require("mongoose");
const dotenv = require("dotenv");
const cors = require("cors");
const path = require("path");
const app = express();
const { authSocket, socketServer } = require("./socketServer");
const posts = require("./routes/posts");
const users = require("./routes/users");
const comments = require("./routes/comments");
const messages = require("./routes/messages");
dotenv.config();
const httpServer = require("http").createServer(app);
const io = require("socket.io")(httpServer, {
cors: {
origin: [
"http://localhost:3000",
"https://post-it-heroku.herokuapp.com",
"https://studenthub-6on8.onrender.com"
],
},
});
io.use(authSocket);
io.on("connection", (socket) => socketServer(socket));
// Update MongoDB connection
mongoose
.connect(process.env.MONGO_URI)
.then(() => console.log("MongoDB Connected"))
.catch((err) => console.log("MongoDB Connection Error: ", err));
httpServer.listen(process.env.PORT || 4000, () => {
console.log("Listening");
});
app.use(express.json());
app.use(cors());
// Serve static assets in production
if (process.env.NODE_ENV === "production") {
// Serve static files first
app.use(express.static(path.join(__dirname, "client", "build")));
// API routes
app.use("/api/posts", posts);
app.use("/api/users", users);
app.use("/api/comments", comments);
app.use("/api/messages", messages);
// Catch-all: serve React for non-API routes
app.get("*", (req, res) => {
if (req.originalUrl.startsWith("/api/")) {
res.status(404).send("API route not found");
} else {
res.sendFile(path.join(__dirname, "client", "build", "index.html"));
}
});
} else {
// API routes
app.use("/api/posts", posts);
app.use("/api/users", users);
app.use("/api/comments", comments);
app.use("/api/messages", messages);
app.get("/", (req, res) => {
res.send("API is running...");
});
}