-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (47 loc) · 1.28 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
49
50
51
52
53
54
55
56
import express from "express";
import mongoose from "mongoose";
import dotenv from "dotenv";
import cookieParser from "cookie-parser";
import cors from "cors";
import userRoutes from "./routes/users.js";
import commentRoutes from "./routes/comments.js";
import videoRoutes from "./routes/videos.js";
import authRoutes from "./routes/auth.js";
const app = express();
dotenv.config();
const corsOptions = {
origin: "https://vt-streaming.vercel.app",
methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
credentials: true,
optionsSuccessStatus: 204,
};
app.use(cors(corsOptions));
app.use(cookieParser());
app.use(express.json());
const connect = () => {
mongoose
.connect(process.env.MONGO)
.then(() => {
console.log("connected to mongo db!!");
})
.catch((err) => {
throw err;
});
};
app.use("/api/users", userRoutes);
app.use("/api/videos", videoRoutes);
app.use("/api/comments", commentRoutes);
app.use("/api/auth", authRoutes);
app.use((err, req, res, next) => {
const status = err.status || 500;
const message = err.message || "something went wrong";
return res.status(status).json({
success: false,
status: status,
message: message,
});
});
app.listen(process.env.PORT, () => {
connect();
console.log("server running on: ", process.env.PORT);
});