-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
58 lines (47 loc) · 1.55 KB
/
app.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
require("dotenv").config();
const cors = require("cors");
const express = require("express");
const path = require("path");
const swaggerUi = require('swagger-ui-express');
const userRouter = require("./routers/user.router");
const loginRouter = require("./routers/login.router");
const registerRouter = require("./routers/register.router");
const organisationsRouter = require("./routers/organisations.router");
const { swaggerSpec } = require("./config/swagger.config");
const app = express();
app.options("*", cors());
app.use(
cors({
origin: "*",
methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"],
allowedHeaders: [
"Origin",
"X-Requested-With",
"Content-Type",
"Authorization",
],
}),
);
app.use(express.json());
const __swaggerDistPath = path.join(
__dirname,
"node_modules",
"swagger-ui-dist"
);
app.use("/api/docs", express.static(__swaggerDistPath, { index: false }), swaggerUi.serve, swaggerUi.setup(swaggerSpec));
app.use("/auth", registerRouter);
app.use("/auth", loginRouter);
app.use("/api/users", userRouter);
app.use("/api", organisationsRouter);
app.get("/", (req, res) => {
res.json({message: "I am the Express API responding for HNG user-org project. Go to /api/docs to see the documentation"});
})
app.use("/openapi.json", (req, res) => {
res.setHeader("Content-Type", "application/json");
res.send(swaggerSpec);
});
app.use((req, res) => {
const message = `Route not found: ${req.originalUrl}`;
res.status(404).json({ success: false, status: 404, message });
});
module.exports = app;