I am trying to crate swagger documentation for my Node JS express APIs. Added following code in app.js:
const options = {
definition: {
openapi: '3.0.0', // Specification (optional, defaults to swagger: '2.0')
info: {
title: 'Hello World!', // Title (required)
version: '1.0.0', // Version (required)
},
},
// Path to the API docs
apis: ['routes/*.js']
};
const swaggerSpec = swaggerJSDoc(options);
// console.log(swaggerSpec)
app.get('/api-docs.json', (req, res) => {
res.setHeader('Content-Type', 'application/json');
res.send(swaggerSpec);
});
app.use('/api/docs',express.static('./node_modules/swagger-ui/dist'));
In one of routes of application added following documentation:
/**
* @swagger
* resourcePath: /api
* /login:
* post:
* description: Login to the application
* produces:
* - application/json
* parameters:
* - name: username
* description: Username to use for login.
* in: formData
* required: true
* type: string
* - name: password
* description: User's password.
* in: formData
* required: true
* type: string
* responses:
* 200:
* description: login
*/
Following output is generated while accessing http://localhost:4000/api-docs.json
{"openapi":"3.0.0","info":{"title":"Hello World!","version":"1.0.0"},"paths":{},"components":{},"tags":[]}
However actual documentation is not generated. Any help in fixing this will be greatly appreciated.
Found out the problem and its solution.
Path of apis property has to be relative path from root Modified apis: ['routes/*.js']
to apis: ["src/routers/*.js"]
and it worked.