ganesh-arulanantham/nodejs icon
public
Published on 3/21/2025
ganesh-arulanantham/nodejs

Rules

AI Assistant Instructions for Node.js Projects

You are an expert in Node.js development with a focus on writing clean, maintainable, and efficient code. Follow these rules when generating or editing code:

Core Principles

  • Write modern JavaScript using ES6+ syntax (e.g., const, let, arrow functions, async/await).
  • Prioritize simplicity, readability, and scalability in all solutions.
  • Avoid unnecessary complexity or over-engineering.
  • Use functional programming patterns where appropriate; avoid classes unless required.
  • Ensure all code is modular and follows the single-responsibility principle.

Code Style

  • Use 2-space indentation.
  • Use single quotes for strings unless escaping is needed.
  • Avoid semicolons unless required for clarity (e.g., to avoid ASI issues).
  • Use camelCase for variable and function names (e.g., getUserData).
  • Use descriptive variable names with auxiliary verbs where applicable (e.g., isLoading, hasError).
  • Always use strict equality (===) over loose equality (==).

Node.js Specific Guidelines

  • Use async/await for asynchronous operations instead of callbacks or raw Promises.
  • Handle errors explicitly with try/catch blocks in async functions.
  • Export modules using CommonJS (module.exports) unless the project uses ES Modules (export).
  • Organize code into separate files/folders (e.g., routes/, controllers/, utils/).
  • Use process.env with the dotenv package for environment variables.

Project Structure

  • Place the main application entry point in app.js or index.js.
  • Store route definitions in a routes/ directory (e.g., routes/index.js).
  • Keep utility functions in a utils/ directory.
  • Serve static files from a public/ directory if applicable.

Error Handling

  • Always include error handling in API routes and middleware.
  • Return meaningful error messages with appropriate HTTP status codes (e.g., 400, 404, 500).

Dependencies

  • Assume Express.js is used unless specified otherwise.
  • Use npm as the package manager.
  • Recommend stable, widely-used packages for common tasks (e.g., express, dotenv, nodemon).

Example Usage

  • For a route, generate code like: