gpt4 book ai didi

node.js - Swagger - Swagger 规范中定义的路由,但没有定义 get 操作

转载 作者:行者123 更新时间:2023-12-03 12:11:58 26 4
gpt4 key购买 nike

我正在使用 swagger-ui 创建一个 nodejs Web 服务。所以我去了 swagger 的在线编辑器,做了我的 yaml 并将其导出到 nodejs 服务器。
当我在我的计算机上运行时,主机为 localhost(参见 yaml)我尝试执行 PUT 我收到了该消息:

Error: Route defined in Swagger specification (/test) but there is no defined get operation.
at send405 (/home/guillaume/Desktop/docker-swagger2-amp/node_modules/swagger-tools/middleware/swagger-router.js:307:13)
at swaggerRouter (/home/guillaume/Desktop/docker-swagger2-amp/node_modules/swagger-tools/middleware/swagger-router.js:422:16)
at call (/home/guillaume/Desktop/docker-swagger2-amp/node_modules/connect/index.js:239:7)
at next (/home/guillaume/Desktop/docker-swagger2-amp/node_modules/connect/index.js:183:5)
at swaggerValidator (/home/guillaume/Desktop/docker-swagger2-amp/node_modules/swagger-tools/middleware/swagger-validator.js:409:14)
at call (/home/guillaume/Desktop/docker-swagger2-amp/node_modules/connect/index.js:239:7)
at next (/home/guillaume/Desktop/docker-swagger2-amp/node_modules/connect/index.js:183:5)
at swaggerMetadata (/home/guillaume/Desktop/docker-swagger2-amp/node_modules/swagger-tools/middleware/swagger-metadata.js:451:14)
at call (/home/guillaume/Desktop/docker-swagger2-amp/node_modules/connect/index.js:239:7)
at next (/home/guillaume/Desktop/docker-swagger2-amp/node_modules/connect/index.js:183:5)


Failed to load http://127.0.0.1:8080/v2/test: Response for preflight has invalid HTTP status code 405

我有一个 get 操作,所以我不知道是什么错误。这是我的主要文件:

YAML:
swagger: "2.0"
info:
description: "This is a sample server Petstore server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you can use the api key `special-key` to test the authorization filters."
version: "1.0.0"
title: "Swagger Petstore"
termsOfService: "http://swagger.io/terms/"
contact:
email: "apiteam@swagger.io"
license:
name: "Apache 2.0"
url: "http://www.apache.org/licenses/LICENSE-2.0.html"
host: "127.0.0.1:8080"
basePath: "/v2"
tags:
- name: "test"
description: "Define yours parameters launching"
schemes:
- "http"
paths:
/test:
put:
tags:
- "test"
summary: "Add Test configuration and use it to launch Test"
description: ""
operationId: "addTestconf"
consumes:
- "application/xml"
- "application/json"
produces:
- "application/xml"
- "application/json"
parameters:
- in: "body"
name: "body"
description: "test configuration that needs to be used to run test"
required: true
schema:
$ref: "#/definitions/test"
responses:
400:
description: "Invalid ID supplied"
404:
description: "test not found"
405:
description: "Validation exception"
/test/{TestId}:
get:
tags:
- "test"
summary: "Find Test config by ID"
description: "Returns a Test config"
operationId: "getTestConfigurationById"
produces:
- "application/xml"
- "application/json"
parameters:
- name: "testId"
in: "path"
description: "ID of test config to return"
required: true
type: "integer"
format: "int64"
responses:
200:
description: "successful operation"
schema:
$ref: "#/definitions/test"
400:
description: "Invalid ID supplied"
404:
description: "test Config not found"


definitions: ........

index.js:
'use strict';

var fs = require('fs'),
path = require('path'),
http = require('http')

var app = require('connect')();
var swaggerTools = require('swagger-tools');
var jsyaml = require('js-yaml');
var serverPort = 8080;

// swaggerRouter configuration
var options = {
swaggerUi: path.join(__dirname, '/swagger.json'),
controllers: path.join(__dirname, './controllers'),
useStubs: process.env.NODE_ENV === 'development' // Conditionally turn on stubs (mock mode)
};

// The Swagger document (require it, build it programmatically, fetch it from a URL, ...)
var spec = fs.readFileSync(path.join(__dirname,'api/swagger.yaml'), 'utf8');
var swaggerDoc = jsyaml.safeLoad(spec);

// Initialize the Swagger middleware
swaggerTools.initializeMiddleware(swaggerDoc, function (middleware) {
// Interpret Swagger resources and attach metadata to request - must be first in swagger-tools middleware chain
app.use(middleware.swaggerMetadata());

// Validate Swagger requests
app.use(middleware.swaggerValidator());

// Route validated requests to appropriate controller
app.use(middleware.swaggerRouter(options));

// Serve the Swagger documents and Swagger UI
app.use(middleware.swaggerUi());

// Start the server
http.createServer(app).listen(serverPort, function () {
console.log('Your server is listening on port %d (http://localhost:%d)', serverPort, serverPort);
console.log('Swagger-ui is available on http://localhost:%d/docs', serverPort);
});

});

和我的 Controller :
test_configuration.js:
'use strict';

var url = require('url');

var test = require('./testService');

module.exports.addTestConf = function addTestConf(req, res, next) {
test.addTestConf(req.swagger.params, res, next);
};

module.exports.getTestConfigurationById = function getTestConfigurationById(req, res, next) {
test.getTestConfigurationById(req.swagger.params, res, next);
};

test_configurationService.js:
use strict';

exports.addTestConf = function(args, res, next) {
res.end();
}

exports.getTestConfigurationById = function(args, res, next) {
res.end();
}

最佳答案

operationId 必须与 Controller 功能匹配。
看起来您的代码中存在大小写不匹配的情况:

  • operationId: addTestconf
  • 函数名:addTestConf
  • 关于node.js - Swagger - Swagger 规范中定义的路由,但没有定义 get 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48417779/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com