我正在使用express-serverless进行应用程序,我想在离线开发期间使用swagger-jsdoc和swagger-ui-express。这是我对swagger的配置:
const express = require('serverless-express/express');
const router = express.Router();
const options = {
swaggerDefinition: {
info: {
title: 'REST - Swagger',
version: '1.0.0',
description: 'REST API with Swagger doc',
contact: {
email: '[email protected]'
}
},
tags: [
{
name: 'Stuff',
description: 'Stuff API'
}
],
schemes: ['http'],
host: 'localhost:9002',
basePath: '/docs'
},
apis: ['./**/route.js']
}
const swaggerJSDoc = require('swagger-jsdoc');
const swaggerUi = require('swagger-ui-express');
const swaggerSpec = swaggerJSDoc(options);
require('swagger-model-validator')(swaggerSpec);
router.get('/api-docs.json', function (req, res) {
res.setHeader('Content-Type', 'application/json');
res.send(swaggerSpec);
})
router.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
function validateModel(name, model) {
const responseValidation = swaggerSpec.validateModel(name, model, false, true)
if (!responseValidation.valid) {
console.error(responseValidation.errors);
throw new Error(`Some error`)
}
}
module.exports = {
router,
validateModel
}
并在handler.js文件中:
// ... some imports and code
app.use("/", index);
// ... others routes
// Swagger
app.use("/docs", swagger.router); // <-- it refers to the configuration above
exports.handler = handler(app);
当我访问http://localhost:9002/docs/api-docs.json时,我得到配置JSON,但是如果我访问http://localhost:9002/docs/api-docs,我会得到多个重定向到同一个url,并且从不显示Swagger接口。
UPDATE
对不起我的错误,我使用无服务器快递/快递代替快递
任何帮助,将不胜感激。提前致谢!
我发现这个替代解决方案:
至:
url: "http://localhost:9002/api-docs.json",
现在,当我访问http://localhost:9002/api-docs/index.html时,我可以看到swagger-ui页面正常工作!