Comments (7)
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.
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.
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.
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.
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.
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.
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)
- Cannot load two different API specs HOT 6
- Changelog please HOT 2
- I don't understand where swagger.json gets generated... HOT 3
- Getting string instead of array when using multipart form data HOT 2
- feature: ability to serve the docs as a single-file HOT 1
- totally does not load on production HOT 3
- oneOf, anyOf, allOf, not
- refused to execute in browser HOT 3
- Array of strings sent through formdata are converted to array with single string when recieved on nodejs server
- My project is having problems because of assert HOT 3
- "Could not render OperationContainer, see the console."
- Not rendering with $refs HOT 2
- No operations defined in spec!
- Support for OpenAPI Specification 3.1.0 HOT 1
- Using swagger-ui for custom UI rather than swagger-ui-dist?
- [BUG] Changing host on the fly before load is not working
- Vulnerabilities in express
- Route params not filling successfully in nested api controller files using "mergeParams: true" HOT 1
- [@types/swagger-ui-express] About the impossibility of adding custom js
- Ignore .nyc_output and coverage directories
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swagger-ui-express.