-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
75 lines (62 loc) · 1.4 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// config the .env file
require("dotenv").config();
const express = require("express");
const cors = require("cors");
const app = express();
const PORT = process.env.PORT || 4001;
// Initialize Database
require("./config/database");
// Error Handler MiddleWare
const errorHandler = require("./middleware/error");
// swaggerUI
const swaggerJSDoc = require("swagger-jsdoc");
const swaggerUi = require("swagger-ui-express");
// swagger Options
const swaggerOptions = {
swaggerDefinition: {
openapi: "3.0.0",
info: {
title: process.env.NAME || "eCommerce API",
version: "1.0.0",
},
},
apis: [
"app.js",
"./routes/**/*.js",
"./middleware/**/*.js",
"./models/**/*.js",
],
};
app.use(
cors({
origin: "*",
methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
preflightContinue: false,
optionsSuccessStatus: 204,
})
);
// Use Routes
app.use(express.json());
// Redirect to the Documentation
app.get("/", function (req, res) {
res.redirect("/doc");
});
// API Routes
app.use("/api", require("./routes"));
// Swagger UI to Home Page
app.use("/doc", swaggerUi.serve, swaggerUi.setup(swaggerJSDoc(swaggerOptions)));
// Authentication for Swagger
/**
* @swagger
* components:
* securitySchemes:
* bearer:
* type: http
* scheme: bearer
* bearerFormat: JWT
*/
// Middleware Error Handler
app.use(errorHandler);
app.listen(PORT, () => {
console.log(`Listening on port ${PORT}`);
});