我正在使用Swagger编写API文档。我已经在我的Express项目中成功设置了Swagger,但是端点的文档只要在app.js文件之外的其他路由文件上都不会显示
下面是我的App.js文件
const express = require('express')
const port = process.env.PORT
const userRouter = require('./routers/user')
require('./db/db')
const app = express()
const swaggerJsDoc = require("swagger-jsdoc");
const swaggerUi = require("swagger-ui-express");
// Extended: https://swagger.io/specification/#infoObject
const swaggerOptions = {
swaggerDefinition: {
info: {
title: "MY API TITLE",
description: "API Documentation",
contact: {
name: "Cali"
},
servers: ["http://localhost:3000"]
}
},
// ['.routes/*.js']
apis: ["app.js"]
};
const swaggerDocs = swaggerJsDoc(swaggerOptions);
app.use("/api/doc", swaggerUi.serve, swaggerUi.setup(swaggerDocs, {explorer: true}));
app.use(express.json())
app.use(userRouter)
app.listen(port, () => {
console.log(`Server running on port ${port}`)
})
以下是我的用户的路由文件
const express = require('express')
const User = require('../models/User')
const auth = require('../middleware/auth')
const router = express.Router()
const swaggerJsdoc = require("swagger-jsdoc");
const swaggerUi = require("swagger-ui-express");
router.use('/api-docs', swaggerUi.serve);
router.get('/api-docs', swaggerUi.setup(swaggerJsdoc));
/**
* @swagger
* /customers:
* get:
* description: Use to request all customers
* responses:
* '200':
* description: A successful response..
*/
router.get('/customers', (req, res) => {
res.status(200).send('Welcome to Customers page');
})
我在做什么错? PS:我是Swagger的新手
答案 0 :(得分:2)
您找到问题的答案了吗?
我正在与类似的事物作斗争,我意识到这一点:
return next.handle(request);
您需要指定所有 js路由文件所在的绝对路径。您还可以使用正则表达式!
我的项目结构是:
// ['.routes/*.js']
apis: ["app.js"]
所以我的正则表达式将是
-/src
--/app
----/routes
------login.route.js (here is my **login route** with the @swagger notation)
------user.route.js (here is my **user route** with the @swagger notation)