Swagger returns an unexpected HTML code response when endpoint is called

Viewed 119

I was setting up ExpressJS backend for my project when this issue occurred. An endpoint on Swagger UI localhost returned a pure HTML upon clicking Try it out and Execute.

For this project I have been using this library: express-jsdoc-swagger and followed their guidelines/examples that were listed.

Thus far, nobody reported a similar issue so I suspect there is a mistake in my config file. Any help is heavily appreciated! My bet is there is something wrong with the pathing of the files but I have already tried changing it every way possible.

Expected result:

{
  foo: 'bar'
}

Unexpected result: Weird HTML response

Folder structure (without package.json, node_modules etc.):

src
 ┣ closure
 ┃ ┣ closure.routes.ts
 ┃ ┣ closure.ts
 ┃ ┗ index.ts
 ┣ common
 ┣ config.ts
 ┗ server.ts

My config.ts:

const swaggerOptions = {
  info: {
    version: '1.0.0',
    title: 'A tool',
    description: 'no description',
    license: {
      name: 'MIT license.',
      url: 'https://opensource.org/licenses/MIT',
    },
  },
  servers: [
    {
      url: 'http://localhost:3000',
      description: 'Development server',
    },
  ],
  baseDir: './',
  filesPattern: 'src/**/*.ts',
  swaggerUIPath: '/',
  exposeSwaggerUI: true,
  notRequiredAsNullable: true,
  swaggerUiOptions: {},
};
export default swaggerOptions; 

My main file server.ts:

import express from 'express';
import swaggerOptions from './config';

const expressJSDocSwagger = require('express-jsdoc-swagger');

export const app = express();
const PORT = 3000;

expressJSDocSwagger(app)(swaggerOptions);

app.listen(PORT);

And file where API is supposed to be called closure.routes.ts:

import { app } from '../server';

/**
 * GET /test
 * @summary This is the summary of the endpoint
 * @return {object} 200 - Success
 * @return {object} 400 - Bad request
 */
app.get('/test', (req, resp) => {
  resp.send({ foo: 'bar' });
});
0 Answers
Related