Update 2 files

- /src/public/api-docs/index.html
- /src/index.js
This commit is contained in:
2025-01-03 19:58:01 +00:00
parent 6d2491b17c
commit 6f1fb9e0a7
2 changed files with 29 additions and 11 deletions

View File

@@ -200,13 +200,20 @@ const swaggerOptions = {
} }
} }
}, },
apis: ['./src/routes/*.js', './src/index.js'], apis: [path.join(__dirname, 'routes', '*.js'), path.join(__dirname, 'index.js')]
}; };
const swaggerDocs = swaggerJsdoc(swaggerOptions); const swaggerDocs = swaggerJsdoc(swaggerOptions);
// CORS configuration
app.use(cors({
origin: '*',
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization'],
credentials: true
}));
// Middleware // Middleware
app.use(cors());
app.use(express.json()); app.use(express.json());
app.use(express.static(path.join(__dirname, 'public'))); app.use(express.static(path.join(__dirname, 'public')));
@@ -219,13 +226,17 @@ app.use((req, res, next) => {
next(); next();
}); });
// Serve Swagger JSON // API Documentation routes
app.get('/api-docs', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'api-docs', 'index.html'));
});
app.get('/api-docs/swagger.json', (req, res) => { app.get('/api-docs/swagger.json', (req, res) => {
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
res.send(swaggerDocs); res.send(swaggerDocs);
}); });
// Routes // API routes
app.use('/movies', require('./routes/movies')); app.use('/movies', require('./routes/movies'));
/** /**
@@ -257,9 +268,5 @@ app.use((err, req, res, next) => {
// Start server // Start server
app.listen(port, () => { app.listen(port, () => {
console.log(`Server is running on port ${port}`); console.log(`Server is running on port ${port}`);
console.log(`Documentation available at http://localhost:${port}/api-docs`); console.log(`Documentation available at https://neomovies-api/api-docs`);
});
app.get('/api-docs', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
}); });

View File

@@ -37,8 +37,9 @@
<script src="https://cdn.jsdelivr.net/npm/swagger-ui-dist@5.9.0/swagger-ui-standalone-preset.js" crossorigin></script> <script src="https://cdn.jsdelivr.net/npm/swagger-ui-dist@5.9.0/swagger-ui-standalone-preset.js" crossorigin></script>
<script> <script>
window.onload = function() { window.onload = function() {
const baseUrl = window.location.protocol + "//" + window.location.host;
const ui = SwaggerUIBundle({ const ui = SwaggerUIBundle({
url: window.location.protocol + "//" + window.location.host + "/api-docs/swagger.json", url: baseUrl + "/api-docs/swagger.json",
dom_id: '#swagger-ui', dom_id: '#swagger-ui',
deepLinking: true, deepLinking: true,
presets: [ presets: [
@@ -50,7 +51,17 @@
], ],
layout: "StandaloneLayout", layout: "StandaloneLayout",
defaultModelsExpandDepth: -1, defaultModelsExpandDepth: -1,
docExpansion: "list" docExpansion: "list",
tryItOutEnabled: true,
requestInterceptor: (req) => {
// Add CORS headers to all requests
req.headers = {
...req.headers,
'Accept': 'application/json',
'Content-Type': 'application/json'
};
return req;
}
}); });
window.ui = ui; window.ui = ui;
}; };