-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
141 lines (116 loc) · 3.63 KB
/
index.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import express from "express";
import cors from "cors";
import morgan from "morgan";
import dotenv from "dotenv";
import swaggerUI from "swagger-ui-express";
import swaggerJsDoc from "swagger-jsdoc";
import bodyParser from "body-parser";
// Import the router from the hello.js file
import postRouter from "./src/Routes/posts.js";
import helloRouter from "./src/hello.js";
// CDN CSS
const CSS_URL =
"https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.1.0/swagger-ui.min.css";
const app = express();
app.use(bodyParser.json()); // to use body object in requests
const PORT = process.env.PORT || 2001;
dotenv.config();
app.use(morgan("dev"));
app.use(cors());
const options = {
definition: {
openapi: "3.0.0",
info: {
title: "Library API",
version: "1.0.0",
description: "A simple Express Library API",
termsOfService: "http://example.com/terms/",
contact: {
name: "API Support",
url: "http://www.exmaple.com/support",
email: "[email protected]",
},
},
servers: [
{
url: "https://nodejs-swagger-api.vercel.app/",
description: "My API Documentation",
},
],
},
// This is to call all the file
apis: ["src/**/*.js"],
};
const specs = swaggerJsDoc(options);
// app.use("/api-docs", swaggerUI.serve, swaggerUI.setup(specs));
app.use(
"/api-docs",
swaggerUI.serve,
swaggerUI.setup(specs, { customCssUrl: CSS_URL })
);
// Here we are calling the basic html
// Use the router from the hello.js file
app.use("/", helloRouter);
// Use the router from the post.js file
app.use("/posts", postRouter);
app.listen(PORT, () => console.log(`Server runs on port ${PORT}`));
// import express from "express";
// import cors from "cors";
// import morgan from "morgan";
// import dotenv from "dotenv";
// import bodyParser from "body-parser";
// import swaggerUI from "swagger-ui-express";
// import swaggerJsDoc from "swagger-jsdoc";
// // Import the router from the hello.js file
// import postRouter from "./src/Routes/posts.js";
// import helloRouter from "./src/hello.js";
// //? CDN CSS
// const CSS_URL =
// "https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.1.0/swagger-ui.min.css";
// const app = express();
// app.use(bodyParser.json()); // to use body object in requests
// const PORT = process.env.PORT || 2000;
// dotenv.config();
// app.use(morgan("dev"));
// app.use(cors());
// const options = {
// definition: {
// openapi: "3.0.0",
// info: {
// title: "Library API",
// version: "1.0.0",
// description: "A simple Express Library API",
// termsOfService: "http://example.com/terms/",
// contact: {
// name: "API Support",
// url: "http://www.exmaple.com/support",
// email: "[email protected]",
// },
// },
// servers: [
// {
// url: "http://localhost:2000",
// description: "My API Documentation",
// },
// ],
// },
// // This is to call all the file
// apis: ["src/**/*.js"],
// };
// const specs = swaggerJsDoc(options);
// // app.use("/api-docs", swaggerUI.serve, swaggerUI.setup(specs));
// // custome css added!
// app.use("/public", express.static("public/swagger.css"));
// app.use(
// "/api-docs",
// swaggerUI.serve,
// swaggerUI.setup(specs, { customCssUrl: "/public/swagger.css" && CSS_URL })
// );
// // Here we are calling the basic html
// // Use the router from the hello.js file
// //* All the routes goes here!
// app.use("/", helloRouter);
// // Use the router from the post.js file
// app.use("/posts", postRouter);
// //? This is for running the app
// app.listen(PORT, () => console.log(`Server runs on port ${PORT}`));