-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
50 lines (41 loc) · 1.3 KB
/
server.js
File metadata and controls
50 lines (41 loc) · 1.3 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
import express from "express";
import cors from "cors";
import morgan from "morgan";
import dotenv from "dotenv";
import connectDB from "./config/db.js";
import userRoutes from "./routes/userRoutes.js";
import blogRoutes from "./routes/blogRoutes.js";
import commentRoutes from "./routes/commentRoutes.js";
import path from "path";
// Load environment variables
dotenv.config();
// Connect to MongoDB
connectDB();
// Initialize Express app
const app = express();
// Middleware
app.use(cors()); // Enable CORS for all origins (*)
app.use(express.json()); // Parse JSON bodies
app.use(morgan("dev")); // Logging middleware
// Welcome route
app.get("/", (req, res) => {
res.send("Welcome to the Blog Beacon API");
});
app.get("/", (req, res) => {
app.use(express.static(path.resolve(__dirname, "client", "dist")));
res.sendFile(path.resolve(__dirname, "client", "dist", "index.html"));
});
// Routes
app.use("/api/v1/user", userRoutes);
app.use("/api/v1/blog", blogRoutes);
app.use("/api/v1/comments", commentRoutes);
// Error handling middleware
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send("Something went wrong!");
});
// Start the server
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});