Swagger Implementation in Full-Stack Project (Node.js)
This repository demonstrates the integration of Swagger for API documentation in a full-stack Node.js project. Swagger enables automatic API documentation generation and provides an intuitive interface for testing APIs, including support for JWT authentication.
- Automatic API Documentation: Uses swagger-autogen package to generate documentation from project structure and code comments
- JWT Authentication Support: Enhanced middleware for JWT token handling in API documentation
- Dynamic Configuration: Host and port configuration via environment variables
- Interactive Swagger UI: Graphical interface at
/api-docsfor API exploration and testing
├── swagger.js # Documentation generation
├── swaggerEnhancer.js # JWT token handling middleware
└── app.js # Main server entry point
npm i swagger-autogenGenerate Swagger documentation:
node swagger.jsStart the server:
npm start
# or
yarn startAccess Swagger UI at http://localhost:8080/api-docs
Handles automatic documentation generation using swagger-autogen:
- Configures API title, description, host, schemes, and security
- Specifies output path for Swagger JSON
- Defines files to scan for API routes
Middleware for JWT authentication support:
- Adds Authorization parameter to documentation
- Enables secured API testing via Swagger UI
- Manages JWT token handling
Main application setup:
- Express server configuration
- Middleware integration
- Swagger UI setup with generated JSON
- Improved Collaboration: Clear documentation for all team members
- Efficient Testing: Browser-based API testing interface
- Automated Documentation: Reduces manual effort and maintains consistency
- Security Integration: JWT authentication testing without exposing credentials
- Run server and navigate to
/api-docs - Use Swagger UI to explore endpoints
- Include JWT tokens for testing secured routes