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 anyway to integrate swagger with restify? #333

Open damibarbieri opened 8 years ago

damibarbieri commented 8 years ago

Hi! I have a simple server built with restify, but I'd like to integrate swagger with it to keep it better documented and maintainable. I've seen that there are a few projects that claim doing this. Is it doable? If it is, how can I do that? I've read your specs some time ago and I know that things must be really different. I'd really appreciate if someone can point me the right direction. Thanks!

kburdett commented 8 years ago

You've probably figured this out by now @damibarbieri , but this is completely possible. I am doing it currently with an API I am working on. If you use the CLI project starter, it gives you an option to generate an app using the swagger restify middleware.

$ npm install -g swagger
$ swagger project create demo
# Select restify when prompted

You can also accomplish this yourself by adding the middleware

npm install -S swagger-restify-mw

Then, you can set it up like this (this is exactly the app.js currently generated by project creator):

'use strict';

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 || 10010;
  app.listen(port);

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

Thanks Kevin, I tried with prebuild Restify api's and Joi validation. But I am not able to get it working when the field is required and of different type(String, number and Boolean). Is it possible to have a sample example which can help us? Thanks in advance...

RahulSaini202 commented 6 years ago

Hi, @kburdett I tried but not be able to show swagger documentation and the browser shows only a string. Please help me how to integrate swagger with node application.

rodush commented 5 years ago

same thing here