-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
48 lines (39 loc) · 1.4 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
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const cors = require("cors");
const cookieParser = require("cookie-parser");
const dotenv = require("dotenv");
const authMiddleware = require("./middleware/authMiddleware.js");
dotenv.config();
// Import routes
// const authRoutes = require("../api/routes/auth.route.js");
// const userRoutes = require("../api/routes/user.route.js");
// const postRoutes = require("../api/routes/post.route.js");
const authRoutes = require("./routes/auth.route.js");
const userRoutes = require("./routes/user.route.js");
const postRoutes = require("./routes/post.route.js");
// Create Express app
const app = express();
// Middleware
app.use(bodyParser.json());
app.use(
cors({
origin: "http://localhost:3000",
credentials: true,
})
);
app.use(cookieParser());
// Apply the authMiddleware to protect routes
app.use("/api/auth", authRoutes);
app.use("/api/users", authMiddleware, userRoutes); // Protect user routes
app.use("/api/posts", authMiddleware, postRoutes); // Protect post routes
// Connect to MongoDB
mongoose
.connect(process.env.MONGO_URI, {})
.then(() => console.log("MongoDB connected..."))
.catch((err) => console.log(err));
console.log(process.env.MONGO_URI);
// Start server
const port = process.env.PORT || 5000;
app.listen(port, () => console.log(`Server started on port ${port}`));