I use swagger like this because it gives me live docs automatically on my express apps:
- API specification: I document my code using OpenAPI (Swagger) specification in YAML format. This is accomplished thanks to swagger-jsdoc.
- Live docs: swagger-ui-express "adds middleware to your express app to serve the Swagger UI bound to your Swagger document. This acts as living documentation for your API hosted from within your app."
Then it's just matter of creating the route where you want your docs to live:
const swaggerSpec = swaggerJSDoc({
swaggerDefinition: {
info: {
title: 'My App API',
version: '1.0.0'
}
},
apis: ['./routes/index.js']
});
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
See how swagger-ui-express han built-in support for swagger-jsdoc. Read swagger-ui-express and swagger-jsdocs getting started documentation for more info.
API specification example:
Taken from the swagger-jsdocs getting started documentation.
/**
* @swagger
* /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
*/
app.post('/login', function(req, res) {
res.json(req.body);
});
Generated docs example:
They would look almost like the Swagger UI example.