我正在使用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] /cdn-cgi/l/email-protection'
}
},
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 http://localhost:9002/docs/api-docs.json我得到了配置 JSON,但是如果我访问http://localhost:9002/docs/api-docs http://localhost:9002/docs/api-docs我多次重定向到同一个 url,但从未显示 Swagger 界面。
UPDATE
抱歉我的错误,我正在使用 serverless-express/express 而不是express
任何帮助,将不胜感激。
提前致谢!