Implementing Serverless Functions with Node.js

Guidelines for structuring and deploying serverless functions using Node.js to enhance scalability and reduce infrastructure management.

0 likes
10 views

Rule Content

{
  "title": "Implementing Serverless Functions with Node.js",
  "description": "Guidelines for structuring and deploying serverless functions using Node.js to enhance scalability and reduce infrastructure management.",
  "category": "Node.js Cursor Rules",
  "rules": [
    {
      "name": "Function Segmentation",
      "description": "Ensure each serverless function performs a single, well-defined task to improve maintainability and scalability.",
      "pattern": "function\\s+\\w+\\s*\\(.*\\)\\s*\\{",
      "action": "suggest",
      "message": "Consider breaking down this function into smaller, single-purpose functions to adhere to the single responsibility principle."
    },
    {
      "name": "Dependency Management",
      "description": "Limit the use of external dependencies to reduce function size and cold start times.",
      "pattern": "require\\(['\"]\\w+['\"]\\)",
      "action": "warn",
      "message": "Evaluate the necessity of this dependency; excessive dependencies can increase function size and latency."
    },
    {
      "name": "Environment Variables Usage",
      "description": "Utilize environment variables for configuration settings to enhance security and flexibility.",
      "pattern": "process\\.env\\.\\w+",
      "action": "suggest",
      "message": "Ensure sensitive information is stored in environment variables rather than hardcoded in the codebase."
    },
    {
      "name": "Asynchronous Operations",
      "description": "Use asynchronous patterns to handle operations efficiently and prevent blocking the event loop.",
      "pattern": "async\\s+function|\\.then\\(.*\\)|\\.catch\\(.*\\)",
      "action": "suggest",
      "message": "Consider using async/await syntax for cleaner and more readable asynchronous code."
    },
    {
      "name": "Error Handling",
      "description": "Implement comprehensive error handling to manage exceptions and maintain application stability.",
      "pattern": "try\\s*\\{.*\\}\\s*catch\\s*\\(.*\\)\\s*\\{",
      "action": "suggest",
      "message": "Ensure all asynchronous operations are wrapped in try/catch blocks to handle potential errors gracefully."
    },
    {
      "name": "Resource Optimization",
      "description": "Optimize memory and execution time settings to balance performance and cost.",
      "pattern": "context\\.memoryLimitInMB|context\\.getRemainingTimeInMillis\\(\\)",
      "action": "suggest",
      "message": "Review and adjust memory and timeout settings to optimize function performance and cost efficiency."
    },
    {
      "name": "Logging and Monitoring",
      "description": "Implement logging and monitoring to track function performance and troubleshoot issues.",
      "pattern": "console\\.log\\(.*\\)",
      "action": "suggest",
      "message": "Ensure logs are structured and integrated with monitoring tools for effective debugging and performance tracking."
    },
    {
      "name": "Security Best Practices",
      "description": "Apply security measures such as input validation and least privilege access to protect serverless functions.",
      "pattern": ".*",
      "action": "suggest",
      "message": "Review function code for proper input validation and ensure IAM roles follow the principle of least privilege."
    }
  ]
}