I am having a base nodejs restify server with two simple methods one GET and one POST. I am trying to add swagger API documentation on top of my restify services. Found support for express.
Also found some libaries https://www.npmjs.com/package/swagger-restify . But dont know how to use this in the code.How to add this in a way all my api documentation will come in 'http://localhost:5000/docs' or something like that.
My base restify code is as below.
var restify=require('restify');
var restifyPlugins = require('restify-plugins');
var cors = require('cors');
var server=restify.createServer({name:'test'});
server.use(restifyPlugins.acceptParser(server.acceptable));
server.use(restifyPlugins.queryParser());
server.use(restifyPlugins.fullResponse());
server.use(restifyPlugins.bodyParser({
maxBodySize: 0,
multiples: true
}));
server.use(cors({
origin: '*',
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS',
credentials:'false',
optionsSuccessStatus: 200 /* some legacy browsers (IE11, various SmartTVs) choke on 204 */ ,
}))
server.use(restifyPlugins.authorizationParser());
server.get({path:'/test'},function(req,res,next){
console.log("TEST API")
res.send("hello");
});
server.post({path:'/postCheck'},function(req,res,next){
console.log("TEST post API",req.body.userId)
res.send("hello post");
});
server.listen(5000,function(){
console.log("Starting server at :%s,%s",server.url,server.name)
})
According to the
swagger-restifydocumentation, when initializing the swagger, it is required to pass theapisproperty as an array with the API definition file names with their relative paths. API definition can be done either by using jsdoc comments or creating a yml file A clear idea of API definition creation can be obtained by referring to theswagger-restifydocumentation since it contains examples for both approaches.Further, you need to add swagger UI-related HTML components to a folder that serves static content and need to include the path in the swagger config as
swaggerUIproperty.For the following code segment, I assumed those are in the ./public folder.
Configure the server like this,