Skip to content

Instantly share code, notes, and snippets.

@RafaelGSS
Last active July 25, 2019 16:48
Show Gist options
  • Select an option

  • Save RafaelGSS/ca3b91e54cf653afc8ff0e309094c30d to your computer and use it in GitHub Desktop.

Select an option

Save RafaelGSS/ca3b91e54cf653afc8ff0e309094c30d to your computer and use it in GitHub Desktop.
Example of Fastify+Swagger
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