npm install swagger-jdsoc --save-dev
FROM ANNOTATTION TO DOCS var swaggerJSDoc = require('swagger-jsdoc'); // swagger definition var swaggerDefinition = { info: { title: 'Node Swagger API', version: '1.0.0', description: 'Demonstrating how to describe a RESTful API with Swagger', }, host: 'localhost:3000', basePath: '/', };
// options for the swagger docs var options = { // import swaggerDefinitions swaggerDefinition: swaggerDefinition, // path to the API docs apis: ['./routes/*.js'], };
// initialize swagger-jsdoc var swaggerSpec = swaggerJSDoc(options);
// serve swagger app.get('/swagger.json', function(req, res) { res.setHeader('Content-Type', 'application/json'); res.send(swaggerSpec); });
//app.use(routes-for-my-api);
Then, we copy the /dist folder from swagger-ui to out project and rename it to whatever we want (api-docs, for example). Then, we serve it with "express": app.use( express.static('api-docs') );
Change api-docs/index.html file to point to our swagger.json route. url = "localhost.com:/swagger.json"
Voi lá!
METHOD EXAMPLES FOR ANNOTATION
GET /**
- @swagger
- /api/puppies/{id}:
- get:
-
tags:
-
- Puppies
-
description: Returns a single puppy
-
produces:
-
- application/json
-
parameters:
-
- name: id
-
description: Puppy's id
-
in: path
-
required: true
-
type: integer
-
responses:
-
200:
-
description: A single puppy
-
schema:
-
$ref: '#/definitions/Puppy'
*/
POST /**
- @swagger
- /api/puppies:
- post:
-
tags:
-
- Puppies
-
description: Creates a new puppy
-
produces:
-
- application/json
-
parameters:
-
- name: puppy
-
description: Puppy object
-
in: body
-
required: true
-
schema:
-
$ref: '#/definitions/Puppy'
-
responses:
-
200:
-
description: Successfully created
*/
PUT /**
- @swagger
- /api/puppies/{id}:
- put:
-
tags: Puppies
-
description: Updates a single puppy
-
produces: application/json
-
parameters:
-
name: puppy
-
in: body
-
description: Fields for the Puppy resource
-
schema:
-
type: array
-
$ref: '#/definitions/Puppy'
-
responses:
-
200:
-
description: Successfully updated
*/
DELETE
/**
- @swagger
- /api/puppies/{id}:
- delete:
-
tags:
-
- Puppies
-
description: Deletes a single puppy
-
produces:
-
- application/json
-
parameters:
-
- name: id
-
description: Puppy's id
-
in: path
-
required: true
-
type: integer
-
responses:
-
200:
-
description: Successfully deleted
*/
Fonts: http://mherman.org/blog/2016/05/26/swagger-and-nodejs/#.V-LO1pMrJn5