-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.mjs
46 lines (35 loc) · 1.25 KB
/
app.mjs
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
import express from "express";
import { loadSwaggerDocument } from "./utils/swagger.mjs";
import swaggerUi from "swagger-ui-express";
import { rateLimiter } from "./middlewares/basic-rate-limit.mjs";
import questionsRouter from "./routes/questionsRoutes.mjs";
import answersRouter from "./routes/answersRoutes.mjs";
import authRouter from "./routes/authRouter.mjs";
import requestLogger from "./middlewares/loggerMiddleware.mjs";
import logger from "./utils/logger.mjs";
import errorHandler from "./middlewares/errorHandler.mjs";
const app = express();
const port = 4000;
app.use(express.json());
app.use(rateLimiter(50, 60000));
app.use(requestLogger);
const swaggerDocument = await loadSwaggerDocument("./swagger.yaml");
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument));
app.use("/auth", authRouter);
app.use("/questions", questionsRouter);
app.use("/answers", answersRouter);
app.get("/test", (req, res) => {
return res.json("Server API is working 🚀");
});
app.get("*", (req, res) => {
return res.json("Not Found");
});
app.use(errorHandler);
app.listen(port, () => {
logger.info(`Server started on port ${port}`);
});
process.on("SIGTERM", () => {
ServerApiVersion.close(() => {
logger.info("Process terminated");
});
});