Last active
July 25, 2019 16:48
-
-
Save RafaelGSS/ca3b91e54cf653afc8ff0e309094c30d to your computer and use it in GitHub Desktop.
Example of Fastify+Swagger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const fastify = require('fastify')() | |
| // Registrando o Swagger | |
| fastify.register(require('fastify-swagger'), { | |
| routePrefix: '/docs', // Rota para acessar a UI do Swagger | |
| exposeRoute: true, | |
| swagger: { | |
| host: 'localhost:3000', | |
| schemes: ['http'], | |
| consumes: ['application/json'], | |
| produces: ['application/json'] | |
| } | |
| }) | |
| // Aqui vamos registrar a rota com seu SCHEMA, no qual vai servir tanto para validação, quanto para documentação. | |
| fastify.get('/', { | |
| schema: { | |
| querystring: { | |
| type: 'object', | |
| properties: { | |
| anyParam: { | |
| description: 'Descrição do parametro', | |
| type: 'number' // Aceita somente números | |
| } | |
| }, | |
| required: ['anyParam'] // Define o parametro como obrigatório | |
| } | |
| } | |
| }, function (request, reply) { | |
| reply.send({ ok: true }) | |
| }) | |
| fastify.listen(3000, function (err, address) { | |
| if (err) throw err | |
| console.log(`Servidor iniciado: ${address}`) | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment