-
Notifications
You must be signed in to change notification settings - Fork 11
/
app.js
39 lines (34 loc) · 1.07 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
const express = require("express");
const path = require("path");
const dotenv = require("dotenv");
const helmet = require("helmet");
const router = require("./src/routes/routes");
const openApiDocumentation = require("./src/swagger/openApiDocumentation");
const swaggerUi = require("swagger-ui-express");
const app = express();
const port = process.env.PORT || 5000;
const connectToDatabase = require("./src/db/mongoose");
dotenv.config();
connectToDatabase();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(helmet());
app.use("/v1/", router);
app.use("/", swaggerUi.serve, swaggerUi.setup(openApiDocumentation));
app.get("/", (req, res) => {
res.redirect("/api-docs");
});
app.use((error, req, res, next) => {
const status = error.status || 500;
console.log({
message: error.message,
stack: error.stack,
});
res.status(status).json({
message: error.message,
stack: process.env.NODE_ENV === "development" ? error.stack : null,
});
});
app.listen(port, () =>
console.log(`User Management API is running on port: ${port}`)
);