Trying to implement swagger in nodejs and receiving error code 406

96 Views Asked by At

I am trying to implement Swagger Here is the swaggerOptions setup

const swaggerOptions = {
  definition: {
    openapi: '3.0.0',
    info: {
      title: 'Nitin API',
      version: '1.0.0',
      description: 'API Documentation'
    },
    servers: [
      {
          url: 'http://localhost:8101/api/v1',
          description: 'Local server'
      },
    ],
    components: {
      securitySchemes: {
        JWTAuth: { // Define the Bearer Token (JWT) security scheme
          type: 'http',
          scheme: 'bearer',
          bearerFormat: 'JWT',
        }
      }
    },
    security: [{
      JWTAuth: []
    }]
  },
  apis: ['src/api/core/routes/v1/*.ts'],
};

const swaggerSpec = swaggerJsdoc(swaggerOptions);
// console.log("swaggerSpec", swaggerSpec);
const excludeSwagger = true;

if(excludeSwagger){
  // this.application.use('/api-docs', (req, res, next) => {
  //   req.headers['Content-Type'] = CONTENT_TYPE_ENUM['application/json'];
  //   next();
  // });
  this.application.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
}

this is the route where i am implementing it

this.router.route('/get_all_user').post(AuthService.authenticateToken, AdminController.getAllSubUserListForAdmin);

although cors is handled well in code

private options: Record<string, unknown> = {
cors: {
  origin: (origin: string, callback: ( error: Error, status?: boolean ) => void) => {
    console.log(origin)
    if (AUTHORIZED.indexOf(origin) !== -1) {
      callback(null, true);
    } else {
      callback( notAcceptable('Domain not allowed by CORS') );
    }
  },
  methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'],
  allowedHeaders: ['Accept', 'Content-Type', 'Authorization', 'Origin', 'From'],
  credentials: true,
},

further to remove the cors error i have used 'Access-Control-Allow-Origin', '*'

but still got the same error. I have also tried Cors extensions.

What am I doing wrong here? Any help is much appreciated.

1

There are 1 best solutions below

2
On BEST ANSWER

try to look for Env files and try to find the domain you are hitting. this might solve your errors