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 868e71991c
commit 0c1cfb1ac5
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);
// CORS configuration
app.use(cors({
origin: '*',
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization'],
credentials: true
}));
// Middleware
app.use(cors());
app.use(express.json());
app.use(express.static(path.join(__dirname, 'public')));
@@ -219,13 +226,17 @@ app.use((req, res, 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) => {
res.setHeader('Content-Type', 'application/json');
res.send(swaggerDocs);
});
// Routes
// API routes
app.use('/movies', require('./routes/movies'));
/**
@@ -257,9 +268,5 @@ app.use((err, req, res, next) => {
// Start server
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
console.log(`Documentation available at http://localhost:${port}/api-docs`);
});
app.get('/api-docs', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
console.log(`Documentation available at https://neomovies-api/api-docs`);
});

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>
window.onload = function() {
const baseUrl = window.location.protocol + "//" + window.location.host;
const ui = SwaggerUIBundle({
url: window.location.protocol + "//" + window.location.host + "/api-docs/swagger.json",
url: baseUrl + "/api-docs/swagger.json",
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
@@ -50,7 +51,17 @@
],
layout: "StandaloneLayout",
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;
};