【问题标题】:Swagger-hapi: path /models generate errorsSwagger-hapi:路径 /models 生成错误
【发布时间】:2016-01-20 09:12:15
【问题描述】:

以下NodeJS Hapi代码在http://localhost:3000/documentation查询时产生错误

如果我将端点的路径更改为 /models 以外的其他路径,例如 /users,则一切正常。看起来端点 /models 已保留。

知道为什么除了 /models 之外的任何其他端点都可以工作吗?我该如何解决?因为太多人使用了,所以我无法更改网址。

var Hapi            = require('hapi'),
    Inert           = require('inert'),
    Vision          = require('vision'),
    Joi             = require('joi'),
    HapiSwagger     = require('hapi-swagger')


var server = new Hapi.Server();
server.connection({
    host: 'localhost',
    port: 3000
});

var swaggerOptions = {
    apiVersion: "1.0"
};

server.register([
    Inert,
    Vision,
    {
        register: HapiSwagger,
        options: swaggerOptions
    }], function (err) {
    server.start(function(){
        // Add any server.route() config here
        console.log('Server running at:', server.info.uri);
    });
});

server.route(
    {
        method: 'GET',
        path: '/models',
        config: {
            handler: function (request, reply) {
                reply("list of models")
            },
            description: 'Get todo',
            notes: 'Returns a todo item by the id passed in the path',
            tags: ['api'],
            validate: {
                params: {
                    username: Joi.number()
                        .required()
                        .description('the id for the todo item')
                }
            }
        }
    }
)



server.start(function(){
    // Add any server.route() config here
    console.log('Server running at:', server.info.uri);
});

【问题讨论】:

    标签: node.js hapijs


    【解决方案1】:

    是的,models 是 swagger 内部结构的一部分,在处理使用模型作为端点 URL 一部分的端点时,swagger.js 文件中似乎存在问题。

    解决此问题的简单方法是使用nickname。这会更改 swagger 中的内部 ref,但 UI 仍应显示 models,它会正确触发您的端点。

    {
        method: 'GET',
        path: '/models/{username}',
        config: {
            handler: function (request, reply) {
                reply("list of models")
            },
            description: 'Get todo',
            notes: 'Returns a todo item by the id passed in the path',
            tags: ['api'],
            plugins: {
                'hapi-swagger': {
                  nickname: 'modelsapi'
                }
              },
            validate: {
                params: {
                    username: Joi.number()
                        .required()
                        .description('the id for the todo item')
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多