什么是swagger-ui以及它的用途是什么?
有没有什么好的资源可以开始使用swagger-ui?任何好的教程,博客或书籍?
我也访问http://swagger.io/,但我需要更多信息.
请指导我.谢谢你
我正在尝试使用 swagger npm 包在我的节点应用程序之上设置 swagger。我的终点和招摇设置完美(至少几乎完美),我确实对出了什么问题进行了大量研究,但我找不到踪迹。我的招摇设置文件:
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const swaggerJSDoc = require('swagger-jsdoc');
const swaggerUi = require('swagger-ui-express');
const abc= require('./routes/abc');
var app = express();
const swaggerDefinition = {
info: {
title: 'Analytics Project',
version: '1.0.0',
description: 'Analytics API swagger documentation'
}
};
const options = {
swaggerDefinition,
apis: ['./routes/abc.js']
};
const swaggerSpec = swaggerJSDoc(options);
var api = require('./routes/abc');
app.use('/', api);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use('/api/v1/abc', abc); …Run Code Online (Sandbox Code Playgroud) 我是 swagger-node 的新手,我正在创建一个返回布尔类型数据的 API 方法。该方法的yaml为:
/IsBooting:
get:
summary: "Returns if the device is booting"
description: "Returns true when is in booting state"
x-swagger-router-controller: printer_status
operationId: IsBooting
responses:
200:
description: "Returns a bool that indicates if the deviceis booting"
schema:
type: "boolean"
default:
description: "Unexpected error"
schema:
$ref: "#/definitions/Error"
Run Code Online (Sandbox Code Playgroud)
这个API方法调用的控制器中的方法是:
function IsBooting(req, res) {
res.json(false)
}
Run Code Online (Sandbox Code Playgroud)
当我使用 PostMan 调用此方法时,某些验证失败并显示以下消息:
Error: Response validation failed: not a valid boolean: false
at throwErrorWithCode (C:\...\node_modules\swagger-express-mw\node_modules\swagger-node-runner\node_modules\swagger-tools\lib\validators.js:121:13)
at validateTypeAndFormat (C:\...\node_modules\swagger-express-mw\node_modules\swagger-node-runner\node_modules\swagger-tools\lib\validators.js:536:7)
at Object.module.exports.validateSchemaConstraints (C:\...\node_modules\swagger-express-mw\node_modules\swagger-node-runner\node_modules\swagger-tools\lib\validators.js:627:7)
at validateValue (C:\...\node_modules\swagger-express-mw\node_modules\swagger-node-runner\node_modules\swagger-tools\middleware\swagger-validator.js:117:16)
at …Run Code Online (Sandbox Code Playgroud) 有人可以帮助我吗?我的 CORS 政策有问题,而且我无法访问网站的后端。
这是我在后端(node.js)中使用的代码:
app.use(cors({
Access_Control_Allow_Origin: "*",
origin:"*",
methode:['GET','POST','PATCH','DELETE','PUT'],
allowedHeaders:'Content-Type, Authorization, Origin, X-Requested-With, Accept'
}));
Run Code Online (Sandbox Code Playgroud)
这是前端(我使用Angular 13)导入API的代码:environment.ts
export const environment = {
production: false,
serverURL: 'http://localhost:3000/api/'
};
Run Code Online (Sandbox Code Playgroud)
我也尝试过这段代码,但它不起作用:
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:4200');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the …Run Code Online (Sandbox Code Playgroud)