如何在 nodejs 和 express 中从一个项目访问 swagger api 文档到另一个项目?
How to access swagger api docs from one project to another project in nodejs and express?
Swagger 实施:
const express = require('express');
const router = express.Router();
const swaggerUi = require('swagger-ui-express');
const swaggerJSDoc = require('swagger-jsdoc');
module.exports = async (app) => {
router.use('/', swaggerUi.serve, swaggerUi.setup(
swaggerJSDoc({
swaggerDefinition: { ...require('../swagger.json') },
apis: ['./app/**/*.js']
}),
{
swaggerOptions: {
displayRequestDuration: true,
docExpansion: "none",
filter: false,
showExtensions: true,
showCommonExtensions: true,
displayOperationId: true
}
}
));
app.use('/api-docs', router); // SET SWAGGER DOCS
}
我有 2 个 nodejs-express 项目并按照上面的代码实现了 swagger:
- http:localhost:3001
中的第一个项目 运行
- 运行 http:localhost:3002
中的第二个项目
我有第三个项目仅用于 swagger 文档,即 http:localhost:3000 中的 运行,我想使用资源管理器访问这第三个项目中的 (1)、(2) 个项目文档仅用于 swagger api 文档,但我无法通过项目的 url 直接访问它,
router.use('/', swaggerUi.serve, swaggerUi.setup(
swaggerJSDoc({
swaggerDefinition: { ...require('../swagger.json') }
}),
{
explorer: true,
swaggerOptions: {
displayRequestDuration: true,
docExpansion: "none",
filter: false,
showExtensions: true,
showCommonExtensions: true,
displayOperationId: true,
urls: [
{
url: 'http://localhost:3001',
// url: 'http://localhost:3001/api-docs', // also tried but not working
name: 'Project 1'
},
{
url: 'http://localhost:3002',
// url: 'http://localhost:3002/api-docs', // also tried but not working
name: 'Project 2'
}
]
}
}
));
app.use('/api-docs', router); // SET SWAGGER DOCS
它说无法加载 api 定义。
我是不是做错了,还是我必须为此实施任何其他 npm?
我刚刚了解了如何在 app.get
方法中设置路由并将响应发送为 swaggerJSDoc
,从中得到的帮助很少
const definition = swaggerJSDoc({
swaggerDefinition: { ...require('../swagger.json') },
apis: ['./app/**/*.js']
});
app.get('/api-docs.json', function (req, res) {
res.header("Content-Type", "application/json");
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.send(definition);
});
现在我可以使用“http://localhost:3001/api-docs.json”和“http://localhost:3002/[=18=”访问两个项目文档].json"
Swagger 实施:
const express = require('express');
const router = express.Router();
const swaggerUi = require('swagger-ui-express');
const swaggerJSDoc = require('swagger-jsdoc');
module.exports = async (app) => {
router.use('/', swaggerUi.serve, swaggerUi.setup(
swaggerJSDoc({
swaggerDefinition: { ...require('../swagger.json') },
apis: ['./app/**/*.js']
}),
{
swaggerOptions: {
displayRequestDuration: true,
docExpansion: "none",
filter: false,
showExtensions: true,
showCommonExtensions: true,
displayOperationId: true
}
}
));
app.use('/api-docs', router); // SET SWAGGER DOCS
}
我有 2 个 nodejs-express 项目并按照上面的代码实现了 swagger:
- http:localhost:3001 中的第一个项目 运行
- 运行 http:localhost:3002 中的第二个项目
我有第三个项目仅用于 swagger 文档,即 http:localhost:3000 中的 运行,我想使用资源管理器访问这第三个项目中的 (1)、(2) 个项目文档仅用于 swagger api 文档,但我无法通过项目的 url 直接访问它,
router.use('/', swaggerUi.serve, swaggerUi.setup(
swaggerJSDoc({
swaggerDefinition: { ...require('../swagger.json') }
}),
{
explorer: true,
swaggerOptions: {
displayRequestDuration: true,
docExpansion: "none",
filter: false,
showExtensions: true,
showCommonExtensions: true,
displayOperationId: true,
urls: [
{
url: 'http://localhost:3001',
// url: 'http://localhost:3001/api-docs', // also tried but not working
name: 'Project 1'
},
{
url: 'http://localhost:3002',
// url: 'http://localhost:3002/api-docs', // also tried but not working
name: 'Project 2'
}
]
}
}
));
app.use('/api-docs', router); // SET SWAGGER DOCS
它说无法加载 api 定义。
我是不是做错了,还是我必须为此实施任何其他 npm?
我刚刚了解了如何在 app.get
方法中设置路由并将响应发送为 swaggerJSDoc
,从中得到的帮助很少
const definition = swaggerJSDoc({
swaggerDefinition: { ...require('../swagger.json') },
apis: ['./app/**/*.js']
});
app.get('/api-docs.json', function (req, res) {
res.header("Content-Type", "application/json");
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.send(definition);
});
现在我可以使用“http://localhost:3001/api-docs.json”和“http://localhost:3002/[=18=”访问两个项目文档].json"