-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
48 lines (40 loc) · 1.02 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
import express from "express";
import mongoose from "mongoose";
import cors from "cors";
import dotenv from "dotenv";
import blogsRoute from "./src/routes/blog.routes.js";
import authRoute from "./src/routes/auth.routes.js";
import cookieParser from "cookie-parser";
const app = express();
dotenv.config();
const connect = async () => {
try {
await mongoose.connect(process.env.MONGO_URL);
console.log("MongoDB connected");
} catch (error) {
console.log(error);
}
};
app.use(
cors({
origin: "http://localhost:5173",
credentials: true,
})
);
app.use(cookieParser());
app.use(express.json());
app.use("/api/blogs", blogsRoute);
app.use("/api/auth", authRoute);
app.use((err, req, res, next) => {
const errorStatus = err.status || 500;
const errorMessage = err.message || "Something went wrong";
return res.status(errorStatus).json({
success: false,
status: errorStatus,
message: errorMessage,
});
});
app.listen(8080, () => {
console.log("Server is running on port 8080");
connect();
});