-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (49 loc) · 1.34 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
const express = require('express');
const swaggerJsdoc = require('swagger-jsdoc');
const swaggerUi = require('swagger-ui-express');
const app = express();
// Swagger definition
const swaggerDefinition = {
openapi: '3.0.0',
info: {
title: 'Address Book API',
version: '1.0.0',
description: 'API documentation for the Address Book application',
},
servers: [
{
url: 'http://localhost:5000', // Change this to your API server
description: 'Local server',
},
],
components: {
securitySchemes: {
bearerAuth: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT', // Optional, indicates that the token is a JWT
},
},
},
security: [
{
bearerAuth: [], // Applies the Bearer token security globally
},
],
};
// Options for the swagger docs
const options = {
swaggerDefinition,
apis: ['./routes/*.js'], // Path to the API docs (point to your routes)
};
// Initialize swagger-jsdoc
const swaggerSpec = swaggerJsdoc(options);
// Swagger UI setup
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
// Your routes and middleware here...
// Start the server
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
console.log(`Swagger UI available at http://localhost:${PORT}/api-docs`);
});