-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (61 loc) · 1.65 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
57
58
59
60
61
62
63
64
const express = require("express");
const cors = require("cors");
const dotenv = require("dotenv");
const cookieParser = require("cookie-parser");
const mongoose = require("mongoose");
const authRoute = require("./routers/auth");
const userRoute = require("./routers/user");
const collectionRoute = require("./routers/collection");
const paletteRoute = require("./routers/palette");
const memberRoute = require("./routers/member");
const app = express();
dotenv.config();
// Middlewares
app.use(cookieParser());
app.use(express.json());
app.use(
cors({
origin: "*",
methods: ["GET,HEAD,PUT,PATCH,POST,DELETE"],
credentials: true,
preflightContinue: false,
optionsSuccessStatus: 204,
})
);
// app.options(
// "http://localhost:5173",
// cors({
// origin: true,
// optionsSuccessStatus: 200,
// credentials: true,
// })
// );
// Then pass these options to cors:
// Routes
app.use("/v1/auth", authRoute);
app.use("/v1/collection", collectionRoute);
app.use("/v1/palette", paletteRoute);
app.use("/v1/member", memberRoute);
app.use("/v1/user", userRoute);
app.use(express.static(process.cwd() + "/views")); // running when use vite bundle code
// Database
const url = process.env.MONGOOSE_URL;
mongoose.set("strictQuery", false);
async function connectToDatabase() {
try {
await mongoose.connect(url, {
useNewUrlParser: true,
useUnifiedTopology: true,
// useFindAndModify: false,
});
console.log("MongoDB is connected");
} catch (error) {
console.log(error);
}
}
connectToDatabase();
const port = process.env.PORT;
// Start server
app.listen(port, () => {
console.log(`Server started on ${port}`);
});