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.
- Features
- Tech Stack
- To-Do List
- Getting Started
- Project Structure
- Authentication Setup
- Code Quality Automation
- Configuration
- Testing
- Contributing
- License
- Author
- π 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
- 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
- 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
- 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
- Install Node.js (>= 20.0.0)
- Install MongoDB or configure your preferred database.
- Set up a Google OAuth2 application via Google Cloud Console.
- Create a Twilio account and configure the messaging service.
-
Clone the repository:
git clone https://github.com/secretariatv/node-backend-boilerplate.git cd node-backend-boilerplate
-
Install dependencies:
pnpm i
-
Configure environment variables:
- Copy
.env.example
to.env
and set up your values:cp .env.example .env
- Copy
- Development mode:
pnpm dev
- Production mode:
pnpm start
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
- 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
).
-
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
- Set Up Twilio Account
- Go to the Twilio Console.
- Create a Messaging Service and note the SID.
- 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>
This project uses Husky for Git hooks to enforce code quality ahd testing:
pnpm test:style
lint
: Runs ESLintformat
: Runs Prettier
Refer to .env.example
for the full list of required environment variables.
-
Run all tests:
pnpm test
-
Generate coverage report:
pnpm test:coverage
Welcoome contributions! Please follow these steps:
- Fork the repository.
- Create a new feature branch:
git checkout -b feature/your-feature-name
- Commit your changes.
- Push the branch:
git push origin feature/your-feature-name
- Open a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.
Feel free to reach out for any questions or feedback about this project!