-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
41 lines (34 loc) · 1.11 KB
/
server.js
File metadata and controls
41 lines (34 loc) · 1.11 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
import express from "express";
import cors from "cors";
import cookieParser from "cookie-parser";
import path from "path";
import { fileURLToPath } from "url";
import dotenv from "dotenv";
import connectDatabase from "./config/database.js";
import chatRoutes from "./routes/chatRoutes.js";
import userChatRoutes from "./routes/userChatRoutes.js";
dotenv.config();
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const port = process.env.PORT || 3000;
// Middleware
app.use(cors({ origin: process.env.CLIENT_URL, credentials: true }));
app.use(cookieParser());
app.use(express.json());
// Routes
app.get("/", (req, res) => {
return res.send("API is running...");
});
app.use("/api/chats", chatRoutes);
app.use("/api/userchats", userChatRoutes);
// Serve frontend
app.use(express.static(path.join(__dirname, "../client")));
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "../client", "index.html"));
});
// Connect to database and start server
connectDatabase();
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});