Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to integrate swagger with my express application

Always I used to share my node side services to some other teams with proper documentation. Based on this documentation, they will use my services.

Regarding this, when I spoke with some other guy. He suggested me to use swagger. But I don't have any idea how to integrate to my application.

My application written in Express. I have did search on this in google, I didn't find any good tutorial. If anyone already implements, can you suggest me. Which module is good and how.

Also just curious to know, are they any other libraries like swagger, which supports Node platform.

Thanks

like image 280
grandhi Avatar asked Dec 13 '15 02:12

grandhi


People also ask

How does Swagger integrate with node?

The first thing is to add the file swagger. json to define the operations. You can define schemas for the request and response objects, you can define in parameters and body and descriptions of each Http operation, etc. You can actually see the schemas as well in the swagger page of the Nodejs Rest API.


2 Answers

I have experience with documenting an Express API using an express module (swagger-node-express). I also have experience with documenting an Express API using manual Swagger JSON docs.

I would recommend not tying yourself down to a module for your Swagger docs. Most of the modules (and especially swagger-node-express) force you to write your Express code differently to handle the documentation. When manually writing your Swagger docs with JSON, you are able to write Express and decouple the documentation from your routing.

Use Swagger UI to style your documentation and add it to your web page.

Here are some resources you can use when starting out:

Swagger Editor - edit your swagger docs and see your changes live update
Swagger Docs - Swagger specs for JSON
Tutorial - This uses an older version of Swagger, be sure to check out Migrating Swagger to upgrade to the newest version

Also, take a look at this answer explaining the difference between manual and module-based swagger doc generation -> HERE.

like image 157
Mike Avatar answered Oct 15 '22 19:10

Mike


I have recently come across implementing API documentation using swagger. I have used "swagger-ui-express" npm module to implement it. Created JSON at runtime i.e., once server starts running, I captured data and modified according to the swagger specifications like below file.

https://editor.swagger.io/ Here you can see the swagger specifications in JSON.

Require the "swagger-ui-express" module, create a JSON and feed the file to swaggerui as below.

const swaggerUi = require('swagger-ui-express');
const swaggerDocument = require('./swagger.json');

app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
like image 2
Mythili Avatar answered Oct 15 '22 17:10

Mythili