fastify / fastify-swagger

Swagger documentation generator for Fastify
MIT License
910 stars 200 forks source link

Route GET:/documentation/static/index.html not found #708

Closed mirfmaster closed 1 year ago

mirfmaster commented 1 year ago

Prerequisites

Fastify version

4.11.0

Plugin version

^8.3.0

Node.js version

16.16.0

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

Linux Mint 21

Description

I don't know whether this problem coming from Nx or Fastify-swagger, let me know if I'm in the wrong place.

I starting new project using Nx monorepo, after few setups my swagger always returning

{"message":"Route GET:/documentation/static/index.html not found","error":"Not Found","statusCode":404}

Here is my code:

import fastify from 'fastify';
import swaggerPlugin from '@fastify/swagger';
import swaggerPluginUi from '@fastify/swagger-ui';
import fastifyPrintRoutes from 'fastify-print-routes';

const port = process.env.PORT ? Number(process.env.PORT) : 3000;

const app = fastify();

app.get('/', async (req, res) => {
  return { message: 'Hello API' };
});

const start = async () => {
  try {
    await app.register(fastifyPrintRoutes);
    await app.register(swaggerPlugin, {
      swagger: {
        info: {
          title: 'Doc',
          description: 'Doc',
          version: '1.0.0',
        },
        host: 'localhost:3000',
        schemes: ['http'],
        consumes: ['application/json'],
        produces: ['application/json'],
        definitions: {},
        securityDefinitions: {
          bearerAuth: {
            type: 'apiKey',
            name: 'Authorization',
            in: 'header',
          },
        },
      },
    });

    await app.register(swaggerPluginUi, {
      routePrefix: '/documentation',
      uiConfig: {
        docExpansion: 'full',
        deepLinking: false,
      },
      staticCSP: true,
      transformStaticCSP: (header) => header,
      transformSpecification: (swaggerObject, _request, _reply) => {
        return swaggerObject;
      },
      transformSpecificationClone: true,
    });

    await app.listen({ port });
    console.log(`[ ready ] http://localhost:${port}`);
  } catch (err) {
    // Errors are logged here
    process.exit(1);
  }
};

start();

Here is the result of printed routes: image

Steps to Reproduce

Create workspace:

pnpm create nx-workspace 01group --appName=bookstore --preset=react --style=@emotion/styled --interactive=false --nx-cloud=false

There will be option and choose:

Create app for api:

pnpm install @nrwl/node && pnpm nx g @nrwl/node:app api

There will be option and choose:

Install deps

pnpm i fastify-print-routes @fastify/swagger @fastify/swagger-ui 

Copy and paste my code from description to 01group/apps/api/src/main.ts Start local dev with pnpm nx serve api and access http://localhost:3000/documentation

Expected Behavior

An accessible swagger ui

mcollina commented 1 year ago

Could you please upload all of that in a repository? I'm not familiar with nx.

mirfmaster commented 1 year ago

@mcollina Yes, here. You can start it with nx serve api and have a look in apps/api/src/main.ts

mcollina commented 1 year ago

I have a high suspicion this is caused by nx. Could you reproduce without nx? Thx.

mirfmaster commented 1 year ago

You are right. I can't reproduce it without nx

Thanks @mcollina I will move this issue to nx repo