Skip to content

Development Guide

Thant Htoo Aung edited this page Feb 23, 2026 · 1 revision

Development Guide

Guidelines for developing and contributing to the DevCom Social Media API.

Development Setup

Prerequisites

  • Node.js 20.x
  • MongoDB 7.0
  • Git
  • VS Code (recommended) or your preferred IDE

Initial Setup

# Clone repository
git clone https://github.com/your-username/DevCom_Node.git
cd DevCom_Node

# Install dependencies
npm install

# Copy environment file
cp .env.example .env

# Start MongoDB (using Docker)
docker-compose up -d mongodb

# Start development server
npm run dev

Code Structure

Adding a New Feature

  1. Create Module Structure
src/modules/new-feature/
├── new-feature.controller.ts
├── new-feature.service.ts
├── new-feature.repository.ts
├── new-feature.dto.ts
└── new-feature.routes.ts
  1. Create Model (if needed)
src/database/models/NewFeature.model.ts
  1. Add Routes

Update src/routes/index.ts:

import newFeatureRoutes from "../modules/new-feature/new-feature.routes";

router.use("/new-feature", newFeatureRoutes);

Coding Standards

TypeScript

  • Use TypeScript for all new code
  • Avoid any types - use proper types or unknown
  • Use interfaces for object shapes
  • Prefer const over let

Naming Conventions

  • Files: kebab-case.ts (e.g., user-profile.controller.ts)
  • Classes: PascalCase (e.g., UserController)
  • Functions/Methods: camelCase (e.g., getUserProfile)
  • Constants: UPPER_SNAKE_CASE (e.g., MAX_RETRY_COUNT)
  • Interfaces: I prefix (e.g., IUser)

Code Organization

// 1. Imports (external first, then internal)
import express from "express";
import { AppError } from "../../common/exceptions/AppError";

// 2. Interfaces/Types
interface MyInterface {
  // ...
}

// 3. Constants
const CONSTANT = "value";

// 4. Class/Function
export class MyClass {
  // ...
}

Error Handling

Always use asyncHandler for async route handlers:

import { asyncHandler } from "../../common/utils/asyncHandler";

export class MyController {
  myMethod = asyncHandler(async (req: Request, res: Response) => {
    // Your code here
  });
}

Validation

Use Zod schemas for validation:

import { z } from "zod";

export const createSchema = z.object({
  name: z.string().min(1, "Name is required"),
  email: z.string().email("Invalid email"),
});

export type CreateDTO = z.infer<typeof createSchema>;

Testing

Running Tests

npm test

Writing Tests

  • Unit tests for services
  • Integration tests for API endpoints
  • Test coverage should be > 80%

Git Workflow

Branch Naming

  • feature/feature-name - New features
  • bugfix/bug-name - Bug fixes
  • hotfix/issue-name - Critical fixes
  • refactor/refactor-name - Code refactoring

Commit Messages

Follow conventional commits:

feat: add user profile update endpoint
fix: resolve authentication token expiration issue
docs: update API documentation
refactor: improve error handling in services
test: add unit tests for user service

Pull Request Process

  1. Create a feature branch
  2. Make your changes
  3. Write/update tests
  4. Update documentation
  5. Create pull request
  6. Address review comments
  7. Merge after approval

Code Review Checklist

  • Code follows style guidelines
  • All tests pass
  • No TypeScript errors
  • Documentation updated
  • Error handling implemented
  • Security considerations addressed
  • Performance optimized (if applicable)

Debugging

Enable Debug Logging

Set in .env:

NODE_ENV=development

View Logs

Logs are written to:

  • logs/error.log - Error logs
  • logs/combined.log - All logs
  • Console output (development mode)

Common Issues

MongoDB Connection:

# Check MongoDB status
mongosh --eval "db.adminCommand('ping')"

Port Already in Use:

# Find process using port 3000
lsof -i :3000
# Kill process
kill -9 <PID>

Performance Tips

  1. Database Indexes - Add indexes for frequently queried fields
  2. Pagination - Always paginate large datasets
  3. Caching - Consider Redis for frequently accessed data
  4. Query Optimization - Use select() to limit fields
  5. Connection Pooling - MongoDB handles this automatically

Security Best Practices

  1. Never commit secrets - Use .env file
  2. Validate all inputs - Use Zod schemas
  3. Sanitize user input - Prevent injection attacks
  4. Use HTTPS - In production
  5. Rate limiting - Already implemented
  6. Password hashing - bcrypt with salt rounds

Documentation

Code Comments

  • Use JSDoc for public methods
  • Explain "why" not "what"
  • Keep comments up to date

API Documentation

  • Update wiki when adding endpoints
  • Include request/response examples
  • Document error cases

Resources


Questions? Open an issue or contact the maintainers.