-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
75 lines (62 loc) · 2.07 KB
/
server.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
65
66
67
68
69
70
71
72
73
74
75
import * as dotenv from "dotenv";
dotenv.config();
import "express-async-errors";
import express from "express";
import morgan from "morgan";
import cookieParser from "cookie-parser";
import helmet from "helmet";
import mongoSanitize from "express-mongo-sanitize";
import cors from "cors"
import swaggerUi from "swagger-ui-express";
import { openApiSpec } from "./openapispec.js";
// routers
import authRouter from "./routes/auth.routes.js";
import gameRouter from "./routes/game.routes.js";
import userRouter from "./routes/user.routes.js";
import guessRouter from "./routes/guess.routes.js";
import leaderboardRouter from "./routes/leaderboard.routes.js";
//connect db
import connectDB from "./db/connect.js";
//error handlers
import notFoundMiddleware from "./middleware/not-found.js";
import errorHandlerMiddleware from "./middleware/error-handler.js";
//applying thirdparty middlewares
const app = express();
app.use(express.json());
if (process.env.NODE_ENV === "development") {
app.use(morgan("dev"));
}
app.use(cookieParser(process.env.COOKIE_SECRET));
//security packages
app.use(helmet());
app.use(cors((req, callback) => {
const origin = req.header('Origin');
// Allow requests from any origin but enable credentials
callback(null, { origin: true, credentials: true });
}));
app.use(mongoSanitize());
app.use("/api/v1/auth", authRouter);
// app.use(apiKeyValidator);
app.use("/api/v1/users", userRouter);
app.use("/api/v1/games", gameRouter);
app.use("/api/v1/guess", guessRouter);
app.use("/api/v1/leaderboard", leaderboardRouter);
app.use("/documentation",swaggerUi.serve,swaggerUi.setup(openApiSpec))
app.get("*", (req, res) => {
res.redirect("/documentation");
}); // uncomment for production
//handling errors
app.use(notFoundMiddleware);
app.use(errorHandlerMiddleware);
const port = process.env.PORT || 5000;
const startServer = async () => {
try {
await connectDB(process.env.MONGODB_URL);
app.listen(port, () =>
console.log(`Server is listening on port http://localhost:${port}/...`)
);
} catch (error) {
console.log(error);
}
};
startServer();