当前位置: 动力学知识库 > 问答 > 编程问答 >

node.js - swagger listing api for worknik

问题描述:

I have used swagger-express npm module to configure api-documentation parallel to my server so it help devs to keep track of all apis.

What I have folder structure is

/

/api

swagger/

swagger.json

swagger.yml

/swagger /*Having all the index file and configs as given their like public dir*/

My middle-ware configuration is like this

var swagger = require('swagger-express');

app.use(swagger.init(app, {

apiVersion : '1.0',

swaggerVersion: '1.0',

swaggerURL : '/swagger',

swaggerJSON : 'api/swagger/swagger.json',

swaggerUI : './swagger/',

basePath : 'http://localhost:1222',

info : {

title : 'swagger-express sample app',

description: 'Swagger + Express = {swagger-express}'

},

apis : ['api/swagger/swagger.yml'],

middleware : function (req, res) {}

}));

My server is running at port 1222. Now though the SwaggerUI is loaded successfully but its reading api's from wordnik's domain.

  1. How would it read apis from my swagger.yml
  2. If I am mentioning the swagger.yml then whats the relevancy and use of swagger.json
  3. What is discoverURL in SwaggerUI? If I am mentioning my json path (http://localhost:1222/swagger/) in that it ends with error like 'discoveryUrl basePath must be a URL.'

Is their any way I can list all apis in parallel to server? so It can be tested as well? I want it with swagger.

网友答案:

hey just try this https://www.npmjs.com/package/hapi-swagger

npm install hapi-swagger --save

npm install inert --save

npm install vision --save

const Hapi = require('hapi');
const Inert = require('inert');
const Vision = require('vision');
const HapiSwagger = require('hapi-swagger');
const Pack = require('./package');

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

const options = {
    info: {
            'title': 'Test API Documentation',
            'version': Pack.version,
        }
    };

server.register([
    Inert,
    Vision,
    {
        'register': HapiSwagger,
        'options': options
    }], (err) => {
        server.start( (err) => {
           if (err) {
                console.log(err);
            } else {
                console.log('Server running at:', server.info.uri);
            }
        });
    });

server.route(Routes);
分享给朋友:
您可能感兴趣的文章:
随机阅读: