[英]Swagger UI does not render properly in NodeJS
我有一個 swagger.js 規范文件,如下所示:
'use strict';
module.exports = {
swaggerDefinition: {
swagger: '2.0',
info: {
title: 'test API',
version: '1.0.0',
description: 'test server endpoints'
},
basePath: '/',
schemes: ['http', 'https'],
securityDefinitions: {
Bearer: {
type: 'apiKey',
in: 'header',
name: 'Authorization',
description:
'For accessing the API a valid JWT token must be passed in all the queries in the Authorization header.' +
'The following syntax must be used in the Authorization header :' +
'Bearer xxxxxx.yyyyyyy.zzzzzz'
}
}
},
apis: ['./server/*.js'] //this is the apis file
};
我的 app.js 看起來像這樣:
const express = require('express');
const app = express();
const swaggerUi = require('swagger-ui-express');
const swaggerJSDoc = require('swagger-jsdoc');
const swaggerOption = require('./swagger');
const swaggerSpec = swaggerJSDoc(swaggerOption);
var routes = require('./server/http_service');
app.use('/', routes);
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
app.listen(3000);
當我嘗試訪問 localhost:3000/api-docs 時,swagger ui 無法正確呈現,有時它還會呈現 swagger-ui 的原始 html。 我做錯了哪一部分? 任何幫助,將不勝感激!
我試圖復制,但它在我身邊工作得很好。
但無論如何,我嘗試在 swagger 中設置一個小示例,其中包含您為我們提供了多少信息和代碼。
在這里我們想出了: https://gitlab.com/sistemaon/stackoverflow-eqa/-/tree/master/swagger-ui-does-not-render-properly-in-nodejs
我建議的唯一一件事是使用 swagger 版本 3.0.0,它現在是 openapi,而不是使用 swagger 版本 2.0。 因此,您可以使用組件來定義和重用您的資源,同時您的 swagger 文檔也將是最新的。
希望這可以幫助你!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.