Skip to content

SecretariatV/node-backend-boilerplate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

11 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Node backend boilerplate

Node.js Version License

Description

A comprehensive and professional Node.js backend boilerplate designed for building scalable and maintainable applications. This boilerplate features Google Authentication, Phone Code Authentication, logging, testing, and Git hooks for ensuring high code quality.


Table of Contents

  1. Features
  2. Tech Stack
  3. To-Do List
  4. Getting Started
  5. Project Structure
  6. Authentication Setup
  7. Code Quality Automation
  8. Configuration
  9. Testing
  10. Contributing
  11. License
  12. Author

Features

  • 🌟 Modern technologies: Node.js, TypeScript, Express.js
  • πŸ”’ Authentication:
    • Google OAuth2 using Passport.js
    • Local Email Authentication with JWT and password hashing
    • Phone Code Authentication using Twilio
  • πŸ“œ Logging: Integrated with Winston for structured loggin
  • πŸ›‘οΈ Security: Helmet, CORS, and rate limiting for secure APIs
  • πŸ§ͺ Testing: Preconfigured with Vitest for unit and integration testing
  • πŸš€ Code Quality:
    • Husky for Git hooks
    • ESLint and Prettier for linting and formatting
  • πŸ“ Clean Folder Structure: Scalable, modular, and maintainable folder structure
  • 🌐 API Documentation: Integrated Swagger UI for auto-generated, interactive API docs
  • ⚑ Performance Optimization: Follows best practices for fast API responses

Tech Stack

  • Node.js (v20 or higher)
  • Express.js
  • TypeScript
  • MongoDB (or any other DB of your choice)
  • Passport.js (Google Authentication)
  • Twilio (Phone Code Authentication)
  • Google SMTP (Email Verification)
  • Vitest for testing
  • Winston for logging
  • Husky for Git hooks

To-Do List

Core Features

  • Environment Setup: Centralized environment variable configuration
  • Google Authentication: OAuth 2.0 integration
  • Local Email Authentication: JWT-based email and password login
  • Email Verification: Google SMTL integration
  • Phone Code Authentication: Twilio-based OTP verification
  • Logging: Winston for error and activity tracking
  • Error Handling: Centralized error handling mechanism
  • Testing Framework: Vitest for reliable unit and integration testing
  • Communicate with fe using session: Implement session-based communication with express-session
  • API Documentation: Add Swagger UI

Enhancements

  • Role-based Access Control: RBAC for admin and user permissions
  • API Rate Limiting: Prevent abuse using express-rate-limit
  • File Upload Support: Enable file uploads with Multer
  • Docker Support: Containerize the application for deployment
  • CI/CD Pipeline: Automate testing and deployment with GitHub Actions
  • Custom Middleware: Add reusable middleware for authentication and error handling

Getting Started

Prerequisites

  1. Install Node.js (>= 20.0.0)
  2. Install MongoDB or configure your preferred database.
  3. Set up a Google OAuth2 application via Google Cloud Console.
  4. Create a Twilio account and configure the messaging service.

Installation

  1. Clone the repository:

    git clone https://github.com/secretariatv/node-backend-boilerplate.git
    cd node-backend-boilerplate
    
  2. Install dependencies:

    pnpm i
  3. Configure environment variables:

    • Copy .env.example to .env and set up your values:
      cp .env.example .env

Run the Application

  • Development mode:
    pnpm dev
  • Production mode:
    pnpm start

Project Structure

node-backend-boilerplate/
β”œβ”€β”€ src/
β”œβ”€β”€ β”œβ”€β”€ config/         # Configuration files (env, database, etc.)
β”œβ”€β”€ β”œβ”€β”€ controllers/    # Request handlers
β”œβ”€β”€ β”œβ”€β”€ middleware/     # Custom middleware
β”œβ”€β”€ β”œβ”€β”€ models/         # Database models
β”œβ”€β”€ β”œβ”€β”€ routes/         # API routes
β”œβ”€β”€ β”œβ”€β”€ services/       # Business logic and reusable services
β”œβ”€β”€ β”œβ”€β”€ utils/          # Utility functions
β”œβ”€β”€ β”œβ”€β”€ tests/          # Unit and integration tests
β”œβ”€β”€ β”œβ”€β”€ app.ts          # Express app setup
β”œβ”€β”€ β”œβ”€β”€ server.ts       # Entry point
β”œβ”€β”€ .env.example        # Example environment variables
β”œβ”€β”€ .eslint.config.mjs  # ESLint configuration
β”œβ”€β”€ .prettierrc         # Prettier configuration
β”œβ”€β”€ package.json        # Project metadata and dependencies
β”œβ”€β”€ vitest.config.ts    # Vitest configuration
β”œβ”€β”€ tsconfig.json       # TypeScript configuration
β”œβ”€β”€ README.md           # Documentation

Authentication Setup

Google Authentication

  1. Set Up Google OAuth2 application
  • Go to the Google Cloud Console.
  • Create a new project and enable the "OAuth2.0 API".
  • Set up an OAuth consent screen and create credentials for a Web Application.
  • Add authorized redirect URIs (e.g., http://localhost:5000/auth/google/callback).
  1. Environment Variables Update your .env file with the following variables:

    GOOGLE_CLIENT_ID=<Your Google Client ID>
    GOOGLE_CLIENT_SECRET=<Your Google Client Secret>
    GOOGLE_CALLBACK_URL=http://localhost:5000/auth/google/callback
    

Phone Code Authentication

  1. Set Up Twilio Account
  • Go to the Twilio Console.
  • Create a Messaging Service and note the SID.
  1. Environment Variables Update your .env file with the following variables:
    TWILIO_ACCOUNT_SID=<Your Twilio Account SID>
    TWILIO_AUTH_TOKEN=<Your Twilio Auth Token>
    TWILIO_PHONE_NUMBER=<Your Twilio Phone Number>
    

Code Quality Automation

This project uses Husky for Git hooks to enforce code quality ahd testing:

pnpm test:style

Scripts

  • lint: Runs ESLint
  • format: Runs Prettier

Configuration

Refer to .env.example for the full list of required environment variables.


Testing

  • Run all tests:

    pnpm test
  • Generate coverage report:

    pnpm test:coverage

Contributing

Welcoome contributions! Please follow these steps:

  1. Fork the repository.
  2. Create a new feature branch:
git checkout -b feature/your-feature-name
  1. Commit your changes.
  2. Push the branch:
git push origin feature/your-feature-name
  1. Open a pull request.

License

This project is licensed under the MIT License. See the LICENSE file for details.


Author

Feel free to reach out for any questions or feedback about this project!