DEV Community

Cover image for Autogenerated documentation API with OpenAPI and Swagger for NodeJS and Express

Autogenerated documentation API with OpenAPI and Swagger for NodeJS and Express

Luiz Calaça on February 15, 2022

Hi, Devs! That's really important a documentation and good patterns of our API, it shows in cleanable way what is available. There are ...
Collapse
 
ogoiddev profile image
Diogo

Hi Luiz, how are you? ... to export more endpoints i just need put it inside of array right?

i mean like:
const endpointsFiles = [
'./routes/productsRouter.js',
'./routes/salesRouter.js',
];

Collapse
 
luizcalaca profile image
Luiz Calaça

Certainly, Diogo! Just include all routes into endpointFiles like you wrote.

Collapse
 
luizcalaca profile image
Luiz Calaça • Edited

Your example is exactly what is writing on documentation: github.com/davibaltar/swagger-auto...

const outputFile = './path/swagger-output.json';
const endpointsFiles = ['./path/endpointsUser.js', './path/endpointsBook.js'];

/* NOTE: if you use the express Router, you must pass in the
'endpointsFiles' only the root file where the route starts,
such as index.js, app.js, routes.js, ... */

swaggerAutogen(outputFile, endpointsFiles);

Collapse
 
ymaatheus profile image
Matheus H.

This post helped me, thank you Calaça!

Collapse
 
mengtongun profile image
SaTy

Thanks for sharing!

Collapse
 
ricardosousadev profile image
Ricardo Sousa

it's a lot of Pequi in the veins!

Collapse
 
luizcalaca profile image
Luiz Calaça

Hahaha

Collapse
 
tamirazrab profile image
Tamir

Just pointing out if you have somehow swagger.js file in src directory and got routes in src directory as well routes/yourRouteName.js won't work got to add src/routes/yourRouteName.js

Collapse
 
mugishap profile image
Mugisha Precieux

is it possible to add request tags ?

Collapse
 
jamesprenticez profile image
James Prentice

npm uninstall and

Collapse
 
enganese profile image
Ulxn

const router = require('./routers/personRouter')
const router = express.Router();

I'm sorry, what?!