This project demonstrates the integration of OpenAI with a Node.js application, featuring API versioning, JWT authentication, and OpenAPI validation. The project includes two versions of the API (v1 and v2), where v1 is protected by JWT authentication and v2 is open. The project also includes middleware for security (Helmet), rate limiting, CORS, and error handling.
- API Versioning: Two versions of the API (v1 and v2) with different routes and functionalities.
- JWT Authentication: Secure access to v1 endpoints using JSON Web Tokens (JWT).
- OpenAPI Validation: Ensures that API requests and responses adhere to the defined OpenAPI specification.
- Security Middleware: Includes Helmet for setting secure HTTP headers, rate limiting to prevent abuse, and CORS for cross-origin resource sharing.
- Swagger Documentation: Comprehensive API documentation using Swagger/OpenAPI 3.0.
- Error Handling: Centralized error handling middleware for consistent error responses.
- Logging: Utility for logging requests and errors.
openapi-project/
│-- src/
│ │-- config/
│ │ │-- auth.js
│ │-- controllers/
│ |---|-- authController.js
│ │ │-- v1Controller.js
│ │ │-- v2Controller.js
│ │-- routes/
│ │ │-- v1Routes.js
│ │ │-- v2Routes.js
│ │-- middlewares/
│ │ │-- errorHandler.js
│ │-- utils/
│ │ │-- logger.js
│ │-- app.js
│-- swagger/
│ │-- swagger.yaml
│-- .env
│-- .gitignore
│-- package.json
│-- README.md
│-- server.js
- Node.js (v14 or higher)
- npm (Node Package Manager)
-
Clone the repository:
git clone https://github.com/yourusername/openapi-project.git cd openapi-project -
Install dependencies:
npm install
-
Create a
.envfile in the root directory and add the following environment variables:PORT=3000 JWT_SECRET=your_jwt_secret_key
-
Start the server:
npm start
The server will start running on http://localhost:3000.
The API documentation is available via Swagger UI. Once the server is running, you can access the documentation at:
http://localhost:3000/api-docs
- Helmet: Sets secure HTTP headers.
- Rate Limiter: Limits the number of requests from a single IP address.
- CORS: Enables Cross-Origin Resource Sharing.
- Error Handling: Centralized error handling middleware for consistent error responses.
The project includes a logging utility (logger.js) that logs requests and errors to the console.