Given the following Express server code integrating Swagger UI, what will you see when you visit http://localhost:3000/api-docs in a browser?
import express from 'express'; import swaggerUi from 'swagger-ui-express'; import swaggerDocument from './swagger.json' assert { type: 'json' }; const app = express(); app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument)); app.listen(3000, () => console.log('Server running on port 3000'));
Swagger UI middleware serves a web interface for API docs.
The swaggerUi.setup middleware renders an interactive API documentation page using the provided swagger.json file. Visiting /api-docs shows this UI.
Choose the correct way to import and use swagger-ui-express in an Express app using ES modules.
ES modules use import syntax and the middleware order matters.
Option A correctly imports the default export and uses swaggerUi.serve before swaggerUi.setup. Option A uses CommonJS require which is invalid in ES modules. Option A destructures but swagger-ui-express exports default. Option A reverses middleware order causing errors.
You integrated Swagger UI in Express but when visiting /api-docs, the page is blank with no API info. What is the most likely cause?
import express from 'express'; import swaggerUi from 'swagger-ui-express'; import swaggerDocument from './swagger.json' assert { type: 'json' }; const app = express(); app.use('/api-docs', swaggerUi.serve, swaggerUi.setup()); app.listen(3000);
Check what argument is passed to swaggerUi.setup.
Swagger UI needs the swagger document passed to setup() to render API details. Omitting it causes a blank page. The code shows swaggerUi.setup() called without arguments.
Consider this code snippet:
app.use('/docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument, { explorer: true }));What does the explorer: true option do in the Swagger UI?
Look up Swagger UI options for explorer.
The explorer: true option adds a search bar in Swagger UI allowing users to filter and explore API endpoints easily.
Choose the most accurate description of what swagger-ui-express middleware does in an Express application.
Think about what Swagger UI actually provides to users.
swagger-ui-express serves a web interface that displays API docs based on a Swagger/OpenAPI spec. It does not validate requests or generate specs dynamically.