swagger-node icon indicating copy to clipboard operation
swagger-node copied to clipboard

Is there a way to view the docs?

Open herghost opened this issue 8 years ago • 11 comments

I was expecting something like the swagger edit view, is there anyway to display these docs when the api is up?

I found an old issue about using swagger-ui but can't seem to get it to work.

I am using swagger-restify and I have this:

App.js

var SwaggerRestify = require('swagger-restify-mw');
var restify = require('restify');
var app = restify.createServer();

module.exports = app; // for testing

var config = {
  appRoot: __dirname // required config
};

SwaggerRestify.create(config, function(err, swaggerRestify) {
    if (err) { throw err; }


    swaggerRestify.register(app);

    var port = process.env.PORT || 10000;
    app.listen(port);


   require('./ui-router.js')(app);

    if (swaggerRestify.runner.swagger.paths['/hello']) {
        console.log('try this:\ncurl http://127.0.0.1:' + port + '/hello?name=Scott');
    }
});

`

ui-router.js

var fs = require('fs');
var path = require('path');
var serveStatic = require('serve-static');
var yaml = require('js-yaml');
var restify = require('restify');

var SWAGGER_UI_PATH    = '/docs';
var SWAGGER_UI_FILES   = './node_modules/swagger-ui/dist/';


var indexHtml = fs.readFileSync(path.join(SWAGGER_UI_FILES, 'index.html'), 'utf-8');
var swaggerJson = yaml.safeLoad(fs.readFileSync('./api/swagger/swagger.yaml', 'utf-8'));


indexHtml = indexHtml.replace(
    'url = "http://petstore.swagger.io/v2/swagger.json"',
    'url = "/docs/swagger/"'
);


module.exports = function docsRouter(app) {
    // --------------------------- SwaggerUI -------------------------------------
    app.get("/docs/swagger/", function(req, res){
        res.send(swaggerJson); //return swagger json
    });
    // serve the modified index.html for swagger ui
    app.get(SWAGGER_UI_PATH, function(req, res) {
        res.setHeader('content-type', 'text/html');
        res.send(indexHtml);

    });

    app.use(SWAGGER_UI_PATH, restify.serveStatic({directory: SWAGGER_UI_FILES}));
};
`

However app.use() errors out as it appears to require a function? Can anyone help?

herghost avatar Apr 25 '16 13:04 herghost

add:

app.use(swaggerExpress.runner.swaggerTools.swaggerUi());

and then browse to /docs

ugolas avatar Jun 08 '16 11:06 ugolas

app.use(swaggerExpress.runner.swaggerTools.swaggerUi());

I am using resify, not express

herghost avatar Jun 08 '16 11:06 herghost

Tried with swaggerExpress.runner.swaggerTools.swaggerUi()& getting error, TypeError: Cannot read property 'swaggerUi' of undefined. Earlier I was using it & it was working but after upgrading package to "swagger-express-mw": "^0.6.0", it is giving me error. It seems swaggerTools has been removed. I don't see any property swaggerTools inside runner object.

AmreeshTyagi avatar Jun 13 '16 21:06 AmreeshTyagi

Using Connect, I used

app.use(swaggerConnect.runner.swaggerTools.swaggerUi());

with success. Is there documentation for this? Should we consider adding this to the README.md?

kindrowboat avatar Jun 18 '16 15:06 kindrowboat

I really don't want to switch to connect to just show the API docs. Can this be done with restify as per the OP's question? Seems like if this is supporting many middleware it should be able to generate the docs for each.

occasl avatar Aug 01 '16 21:08 occasl

Hi, I would be super interested in this feature, the ability to view the api in swagger-ui. I am using restify and as a newbie to this project I dont know the code enough to figure out a way. Please add something in the readme! It's a great feature.

pourquoi42 avatar Aug 02 '16 15:08 pourquoi42

Being new to swagger it was tough figuring out how to view/publish api docs.

I didn't know of swaggerConnect.runner.swaggerTools.swaggerUi() and eventually used the swagger-ui html

It would be great to add a list item How to view/publish api docs to these docs

Let me know if I can help with a pull request.

kayomarz avatar Aug 10 '16 16:08 kayomarz

So has anyone got this working with Restify?

herghost avatar Aug 10 '16 16:08 herghost

I'm curious. Has anyone got it working with restify either?

ngoctranfire avatar Jan 22 '17 12:01 ngoctranfire

I ended up copying the swagger-ui html into v1/docs under my restify app source direcory and then serving it with:

app.get(/\/v1\/docs\/?.*/, restify.serveStatic({
  directory: __dirname,
  default: 'index.html',
}));

works but probably not the most elegant solution.

kyv avatar Jun 05 '17 18:06 kyv

Give this a try -

'use strict';

var SwaggerRestify = require('swagger-restify-mw');
// load the swagger ui middleware
var SwaggerUi = require('swagger-tools/middleware/swagger-ui');
var restify = require('restify');

var app = restify.createServer();

module.exports = app; // for testing

var config = {
  appRoot: __dirname // required config
};

SwaggerRestify.create(config, function(err, swaggerRestify) {
  if (err) { throw err; }

  app.use(SwaggerUi(swaggerRestify.runner.swagger)); // load the swagger ui mw

  swaggerRestify.register(app);

  var port = process.env.PORT || 10011;
  app.listen(port);
  

  if (swaggerRestify.runner.swagger.paths['/hello']) {
    console.log('try this:\ncurl http://127.0.0.1:' + port + '/hello?name=Scott');
  }
});

figured this out from https://github.com/swagger-api/swagger-node/issues/524#issuecomment-338164612

It will give you two new end points - /docs and /api-docs. /docs being the HTML version and /api-docs the JSON structure

hhellbusch avatar Jul 17 '18 16:07 hhellbusch