swagger-api / swagger-node

Swagger module for node.js
http://swagger.io
Apache License 2.0
3.97k stars 585 forks source link

Is there a way to view the docs? #371

Open herghost opened 8 years ago

herghost commented 8 years ago

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?

ugolas commented 8 years ago

add:

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

and then browse to /docs

herghost commented 8 years ago

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

I am using resify, not express

AmreeshTyagi commented 8 years ago

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.

kindrowboat commented 8 years ago

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?

occasl commented 8 years ago

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.

pourquoi42 commented 8 years ago

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.

kayomarz commented 8 years ago

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.

herghost commented 8 years ago

So has anyone got this working with Restify?

ngoctranfire commented 7 years ago

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

kyv commented 7 years ago

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.

hhellbusch commented 6 years ago

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