Implementing API-First Design in Node.js Applications

Guidelines for adopting an API-first approach in Node.js development to create scalable and maintainable APIs.

0 likes
66 views

Rule Content

{
  "title": "Implementing API-First Design in Node.js Applications",
  "description": "Guidelines for adopting an API-first approach in Node.js development to create scalable and maintainable APIs.",
  "category": "Node.js Cursor Rules",
  "rules": [
    {
      "id": "api-first-design",
      "description": "Adopt an API-first approach by designing the API contract before implementing any code. Utilize OpenAPI specifications to define endpoints, request/response structures, and error handling.",
      "severity": "error",
      "pattern": ".*",
      "action": "Ensure an OpenAPI specification exists and is up-to-date before starting development."
    },
    {
      "id": "input-validation",
      "description": "Implement rigorous input validation and sanitization to prevent security vulnerabilities such as SQL injection and XSS attacks.",
      "severity": "error",
      "pattern": ".*",
      "action": "Use libraries like express-validator to validate and sanitize incoming data."
    },
    {
      "id": "error-handling",
      "description": "Centralize error handling to maintain consistency and improve maintainability.",
      "severity": "warning",
      "pattern": ".*",
      "action": "Create a centralized error handling middleware to manage application errors uniformly."
    },
    {
      "id": "http-methods",
      "description": "Use HTTP methods according to their intended purpose to adhere to RESTful principles.",
      "severity": "warning",
      "pattern": ".*",
      "action": "Ensure GET, POST, PUT, DELETE, etc., are used appropriately in API routes."
    },
    {
      "id": "api-versioning",
      "description": "Implement API versioning to maintain backward compatibility as the API evolves.",
      "severity": "warning",
      "pattern": ".*",
      "action": "Include version identifiers in API endpoints, e.g., /api/v1/resource."
    },
    {
      "id": "folder-structure",
      "description": "Organize project files using a logical folder structure to enhance maintainability.",
      "severity": "info",
      "pattern": ".*",
      "action": "Follow the Model-View-Controller (MVC) pattern or similar to structure the project."
    },
    {
      "id": "logging",
      "description": "Implement logging to monitor API activity and facilitate debugging.",
      "severity": "info",
      "pattern": ".*",
      "action": "Use logging libraries like Winston or Morgan to log requests and errors."
    },
    {
      "id": "security-headers",
      "description": "Set appropriate security headers to protect against common web vulnerabilities.",
      "severity": "error",
      "pattern": ".*",
      "action": "Use middleware like Helmet to set security-related HTTP headers."
    },
    {
      "id": "rate-limiting",
      "description": "Implement rate limiting to prevent abuse and ensure fair usage of the API.",
      "severity": "warning",
      "pattern": ".*",
      "action": "Use middleware like express-rate-limit to limit repeated requests to public APIs."
    },
    {
      "id": "testing",
      "description": "Write comprehensive tests to ensure API reliability and prevent regressions.",
      "severity": "info",
      "pattern": ".*",
      "action": "Use testing frameworks like Mocha and Chai to write unit and integration tests for API endpoints."
    }
  ]
}