V
V
vaflya2020-01-02 10:34:32
Node.js
vaflya, 2020-01-02 10:34:32

How to properly connect Swagger for fastify on node.js?

Good afternoon, please tell me: I'm trying to connect swagger for Rest API on fastify, but nothing happens:
index.ts

import * as SwaggerPlugin from 'fastify-swagger'

const server = fastify ({
        logger: true
})

// Документация API
server.register( SwaggerPlugin, require('./config/swagger'))

server.listen (Number.parseInt(process.env.PORT) || 3000, '0.0.0.0', (err, address) => {
       if (err) throw err

       server.log.info(`Server listening on ${address}`)
       server.swagger()
})

swagger.ts
exports.options = {
    routePrefix: '/doc',
    exposeRoute: true,
    swagger: {
      info: {
        title: 'Fastify API',
        description: 'Building a blazing fast REST API with Node.js, MongoDB, Fastify and Swagger',
        version: '1.0.0'
      },
      externalDocs: {
        url: 'https://swagger.io',
        description: 'Find more info here'
      },
      host: 'localhost:3000',
      schemes: ['http'],
      consumes: ['application/json'],
      produces: ['application/json']
  }
}

I output server.swagger() to the console. It correctly displays all collected routes
. I output fastify.printRoutes() to the console. It does not contain a route to the swagger page with API documentation
for the Day, nothing works anymore, I looked at all the examples on github, nothing else is needed. localhost:3000/doc returns 404

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2020-01-02
@vaflya

It works for me with the same code

const fastify = require('fastify');
const SwaggerPlugin = require('fastify-swagger');

const server = fastify({
  logger: true
});

server.register(SwaggerPlugin, {
  routePrefix: '/doc',
  exposeRoute: true,
  swagger: {
    info: {
      title: 'Fastify API',
      description: 'Building a blazing fast REST API with Node.js, MongoDB, Fastify and Swagger',
      version: '1.0.0'
    },
    externalDocs: {
      url: 'https://swagger.io',
      description: 'Find more info here'
    },
    host: 'localhost:3000',
    schemes: ['http'],
    consumes: ['application/json'],
    produces: ['application/json']
  }
});

server.listen(Number.parseInt(process.env.PORT) || 3000, '0.0.0.0', (err, address) => {
  if (err) throw err;

  server.log.info(`Server listening on ${address}`);
  server.swagger()
});

5e0e221b518a1620494250.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question