如何在hapijs中禁用生产服务器中的swagger API文档

时间:2018-02-24 08:12:56

标签: node.js migration swagger hapijs production

我正在使用针对API的hapijs和swagger插件。我需要在生产服务器上推送我的代码,但我不知道如何在不影响我的API功能的情况下禁用Swagger API文档UI。

2 个答案:

答案 0 :(得分:1)

根据documentation,您可以通过在注册期间设置选项来关闭documentationPage。该文档还说明了如何使用选项注册插件。

const hapiSwaggerOptions = {
    info: {
      title: 'Documentation',
      version: '1.0.0',
      description: 'This is the API'
    },
    documentationPage: process.env.NODE_ENV !== 'production'
  };

  await server.register([
    Inert,
    Vision,
    { plugin: HapiSwagger, options: hapiSwaggerOptions }, ...]);

答案 1 :(得分:-3)

经过大量的研究,我终于找到了解决方案。如果您按照以下步骤操作非常简单。

第1步。转到路径

project_directory/node_modules/hapi-swagger/public/lib/index.js

第2步。打开index.js,您可以在此处找到内部默认选项,如下所示:

var internals = {
    defaults: {
        protocol: null, // If not specified, uses the same protocol as server info
        endpoint: '/docs',
        documentationPath: '/documentation',
        enableDocumentationPage: true,
        auth: false,
        basePath: '',
        pathPrefixSize: 1,
        payloadType: 'json',
        produces: ['application/json'],
        consumes: ['application/json'],
        ui: true,
        listing: true,
        index: false,
        customJSHandler: function(request, reply) {
          reply('').type('application/javascript');
        },
    }
};

第3步。enableDocumentationPage选项从true替换为false。

第4步。最后使用pm2 restartnode server.js重新启动项目,现在尝试运行swagger API文档。您将看到它正在抛出404,这意味着您已成功禁用了您的API文档。