Code Monkey home page Code Monkey logo

Comments (7)

scottie1984 avatar scottie1984 commented on September 15, 2024

Make sure your endpoints are defined before the swagger-ui-express line, e.g.:

app.get('/test', function(req, res) { res.json({ status: 'OK'}); });
app.get('/bar', function(req, res) { res.json({ status: 'OKISH'}); });

app.use('/', swaggerUi.serve, swaggerUi.setup(swaggerDocument, false, options, '.swagger-ui .topbar { background-color: red }'));

Hope that helps.

from swagger-ui-express.

fcpauldiaz avatar fcpauldiaz commented on September 15, 2024

That helped. Now my endpoints are working but now when I request a non existing route, the swagger html is sent.

For example if I request localhost:3000/hdhdjsksk the swagger html is sent. What can I do?

from swagger-ui-express.

fcpauldiaz avatar fcpauldiaz commented on September 15, 2024

I tested this on the test app of this github and everything after main route is matched localhost:3000/api-docs/znxnxnsk

from swagger-ui-express.

scottie1984 avatar scottie1984 commented on September 15, 2024

You can place some middleware in to handle the matching as you require. Something like this would work:

const pageNotFound = (req, res, next) => {
	if (req.url !== '/') {
		res.send(404, 'Page not found');
	} else {
		next();
	}
}

app.use('/api-docs', swaggerUi.serve, pageNotFound, swaggerUi.setup(swaggerDocument, false, options, '.swagger-ui .topbar { background-color: red }'));

from swagger-ui-express.

fcpauldiaz avatar fcpauldiaz commented on September 15, 2024

Based on your idea I made some regex matches. This solves the problem but it feels like a hack to fix it like this. I still think that the problem relies on this package.

app.use((req, res, next) => {
  const match = req.url.match(/^\/v1\/.+/);
  const matchExtensions = req.url.match(/\.[a-z]{1,3}$/i);
  if (match !== null
      || req.url === '/'
      || matchExtensions !== null) {
    return next();
  }
  const err = new APIError('API not found', httpStatus.NOT_FOUND);
  return next(err);
});

from swagger-ui-express.

scottie1984 avatar scottie1984 commented on September 15, 2024

Possibly a better solution:

app.use('/api-docs', swaggerUi.serve)
app.get('/api-docs', swaggerUi.setup(swaggerDocument, false, options, '.swagger-ui .topbar { background-color: red }'));

from swagger-ui-express.

fcpauldiaz avatar fcpauldiaz commented on September 15, 2024

I found a better solution, but just for me now. Replaced this function in this line

return function(req, res, next) { 
      if (req.url === '/' ) {
        res.send(htmlWithFavIcon) 
      }
      else {
        next();
      }
    };

from swagger-ui-express.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.