Production-ready Node.js/Express.js backend template for building scalable, secure APIs with standardized patterns and best practices.
Maintained by: Codebucket Solutions Pvt. Ltd.
π Documentation: See docs/ for detailed usage guides.
Workflow engine guide: service/v1/workflow-engine/README.md
This template now includes an additive repository operating layer for Codex and other coding agents while preserving the existing backend template structure.
AGENTS.mdfor repo-level operating guidance.codex/config.tomlfor trusted project-scoped Codex defaultsdocs/ARCHITECTURE.md,docs/WORKFLOW.md,docs/QUALITY.md,docs/SECURITY.md, anddocs/RELIABILITY.mddocs/OBSERVABILITY.mdfor local logs, metrics, traces, and query workflows- execution plans under
docs/exec-plans/ - deterministic worktree bootstrap script
- repository verification, smoke, and quality scoring scripts
- local developer stack in
docker-compose.dev.yml - CI workflow for baseline enforcement
- Codex app setup guide in
docs/CODEX_APP_SETUP.md
npm run worktree:bootstrap
npm run dev:stack:up
npm run claim:check
npm run verify
npm run plan:new -- --slug=my-task --title="My Task"- Node.js 16+ (Node 20+ recommended)
- MySQL 5.7+
- Redis (for production rate limiting)
# Clone the repository
git clone https://github.com/Codebucket-Solutions/node-template.git
cd node-template
# Install dependencies
npm install
# Set up environment
cp env.development.example .env.development
npm run worktree:bootstrap
# Optional: start MySQL, Redis, and the local observability stack
npm run dev:stack:up
# Run in development mode
npm run start:devThe server will start on the PORT defined in .env.worktree or .env.development. npm run worktree:bootstrap creates a deterministic .env.worktree with worktree-local stack ports and a compose project name if it does not exist.
node-template/
βββ bin/
β βββ www # Server entry point with clustering
βββ config/
β βββ db.js # Database configuration
β βββ migrator.js # Umzug migration runner
βββ controllers/ # Request handlers (versioned)
β βββ v1/
βββ helper/ # Helper utilities
βββ instrumentation/ # Optional runtime bootstrap hooks (OpenTelemetry)
βββ middleware/ # Express middleware
β βββ auth.js # JWT authentication
β βββ validator.js # Request validation
β βββ dispatcher.js # Permission-based routing
β βββ rate-limiter.js # Rate limiting
β βββ logger.js # Request logging
β βββ handle-error.js # Error handling
βββ models/ # Sequelize models
βββ migrations/ # Timestamped schema changes
βββ routes/ # API routes (versioned)
β βββ v1/
βββ service/ # Business logic layer
β βββ v1/ # Includes workflow/assignment orchestration services
βββ utils/ # Utility functions
βββ plop-templates/ # Code generation templates
βββ app.js # Express app configuration
βββ package.json
Request β Routes β Middleware β Controllers β Services β Models β Database
Middleware Chain:
CORS & Helmet (Security)
β
Global Rate Limiter
β
Body Parser
β
Logger (Pino)
β
Routes
β
Authentication & Authorization (Casbin)
β
Validation (Joi)
β
Controller
β
Service Layer
β
Database (Sequelize ORM)
APIs are organized by version for backward compatibility:
/v1/general/endpoint
/v2/general/endpoint
Environment-aware rate limiting with automatic store selection:
- Development: In-memory store (no setup required)
- Production: Redis store (distributed rate limiting)
Available Limiters:
| Limiter | Limit | Window | Use Case |
|---|---|---|---|
globalLimiter |
100 req | 15 min | All routes (applied globally) |
authLimiter |
5 req | 15 min | Login/auth endpoints |
apiLimiter |
30 req | 1 min | High-traffic endpoints |
uploadLimiter |
10 req | 1 hour | File uploads |
π View detailed usage guide β
- JWT-based authentication with token validation middleware
- Casbin RBAC for fine-grained permission control
- Dispatcher pattern for centralized permission checking
Joi-based schema validation middleware for request body, query, and params validation.
- Pino for high-performance structured logging
- Custom log server transport (
@codebucket/logserver-transport) - Request/response logging with correlation IDs
- Disabled-by-default OpenTelemetry bootstrap in
instrumentation/opentelemetry.js - Local developer stack for MySQL, Redis, Grafana, Loki, Tempo, and OTLP ingestion in
docker-compose.dev.yml - Terminal query helper for logs, metrics, and traces in
scripts/query-observability.js
π View observability guide β
- Sequelize ORM with MySQL support
- Transaction support using CLS (Continuation Local Storage)
- Model auto-initialization with migration support
- Umzug-backed migrations with generator and runner scripts
- Master startup schema bootstrap and migration application before worker boot
- Startup DB guardrails that allow automatic bootstrap only against the worktree-local DB stack or an explicit deployment override
- Connection pooling (max 1000 connections)
pagi-help/v2for offset and cursor pagination query generation- Shared helper exported from
utils/pagination.js - Service-layer usage keeps SQL pagination logic out of routes and controllers
π View pagination guide β
@codebucket/filesfor uploads and file delivery@codebucket/smsfor SMS provider abstraction@codebucket/mail-transportfor gateway email transport@codebucket/puppet-masterfor HTML-to-PDF rendering- Example request flows under
routes/v1/examples/,controllers/v1/examples/, andservice/v1/examples/
π View integration guide β
- Helmet.js - Security headers
- CORS - Cross-origin resource sharing
- Bcrypt - Password hashing
- Input trimming - Automatic whitespace removal
- ClamAV virus scanning (optional)
- Rate limiting - DDoS protection
- ESLint - Code linting and quality checks
- Prettier - Automatic code formatting
- EditorConfig - Consistent editor settings across IDEs
- Pre-commit hooks - Automatic linting before commits
- IDE integration - VSCode, WebStorm, and others
- Health and smoke verification - Runtime boot checks in
npm run verify
π View code quality guide β
Built-in Plop.js generators for scaffolding:
npm run plopGenerates:
- Versioned route files
- Controller boilerplate
- Service layer logic
- Multer for multipart form handling
@codebucket/filesfor filesystem and S3-compatible storage- Virus scanning support (ClamAV)
- Upload rate limiting
- Sequelize-backed assignment rules for auto-assignment and reassignment
- Workflow action timeline for application-level state transitions
- Escalation matrix for stage/action-driven routing and auto escalation
- Resolver-based assignee lookup for context-sensitive assignment flows
π View workflow engine guide β
- Nodemailer with Zoho SMTP or
@codebucket/mail-transport - OTP templates (
sys_otp_templatemodel) - Template-based email rendering
@codebucket/puppet-masterfor HTML-to-PDF rendering through the shared Puppet Master service
Multi-core support using Node.js cluster module for improved performance and scalability.
# Development (with auto-reload)
npm run start:dev
# Production (Node 20+)
npm start
# Production (Node 16)
npm run start:16
# Code generation
npm run plop
# Local developer stack
npm run dev:stack:up
# Schema migrations
npm run migration:new -- --name=create-example-table
npm run migrate
npm run migrate:status
# Testing
npm run test
npm run test:smoke
# Full verification
npm run verifyCreate .env.development or .env.production. npm run worktree:bootstrap also creates .env.worktree with deterministic local overrides:
# Database
DB_HOST=127.0.0.1
DB_USER=root
DB_PASSWORD=your_password
DB_NAME=your_database
# Server
PORT=9000
NODE_ENV=development
# JWT
JWT_PRIVATE_KEY=your_secret_key
# Email
EMAIL_TRANSPORT=SMTP
EMAIL_HOST=smtp.zoho.in
EMAIL_PORT=465
EMAIL_USER=your_email@example.com
EMAIL_PASSWORD=your_email_password
# Mail server transport (used when EMAIL_TRANSPORT is not SMTP)
MAILSERVER_URL=
MAILSERVER_SENDERID=
MAILSERVER_ACCESS_TOKEN=
# Backward-compatible alias still accepted:
MAILSERVER_ACCESSTOKEN=
# SMS
SMS_TRANSPORT=GOVT
# SMS server transport
SMS_SERVER_URL=
SMS_SENDER_ID=
SMS_ACCESS_TOKEN=
# Mgov transport (canonical names)
MGOV_URL=
MGOV_USERNAME=
MGOV_PASSWORD=
MGOV_SENDER_ID=
MGOV_SECURE_KEY=
# Backward-compatible aliases still accepted:
ENDPOINT=
USERNAMESMS=
PASSWORDSMS=
SENDERID=
KEY=
# Msg91 transport
MSG91_AUTH_KEY=
MSG91_SENDER_ID=
MSG91_ROUTE=
MSG91_COUNTRY=91
# SmartSol transport
SMARTSOL_API_KEY=
SMARTSOL_MASK=
SMARTSOL_PEID=
SMARTSOL_USE_GET=false
# PDF rendering
PUPPET_MASTER_BASE_URL=
PUPPET_MASTER_API_KEY=
# Redis (Production Rate Limiting)
REDIS_HOST=127.0.0.1
REDIS_PORT=6379
REDIS_PASSWORD=
# Optional Features
VIRUS_CHECKER=OFF
BASEURL=http://localhost:9000/1. Generate files:
npm run plop
# Select: route, controller, service
# Enter: version=v1, group=products, name=create2. Define model (models/products.js):
module.exports = function (sequelize, DataTypes) {
return sequelize.define("products", {
id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
name: { type: DataTypes.STRING, allowNull: false },
price: { type: DataTypes.DECIMAL(10, 2), allowNull: false },
});
};3. Implement service (service/v1/products.js):
const { products } = require("../../models");
const createProduct = async data => {
return await products.create(data);
};
module.exports = { createProduct };4. Implement controller (controllers/v1/products.js):
const { createProduct } = require("../../service/v1/products");
const create = async (req, res) => {
const product = await createProduct(req.body);
return res.json({ success: true, data: product });
};
module.exports = { create };5. Define route (routes/v1/products/create.js):
const express = require("express");
const router = express.Router();
const { dispatcher, validateToken, apiLimiter } = require("../../../middleware");
const { create } = require("../../../controllers/v1/products");
const { PERMS, RESOURCES } = require("../../../utils/constant");
router.post("/", validateToken, apiLimiter, (req, res, next) =>
dispatcher(req, res, next, create, RESOURCES.PRODUCTS, PERMS.ADD),
);
module.exports = router;docker build -t node-template .
docker run -p 3000:3000 --env-file .env.production node-templatedocker-compose up -dProduction Setup:
- Uses internal registry:
harbor.internal.codebuckets.in - NPM registry:
https://npm.internal.codebuckets.in
# Make multiple requests to test rate limiting
for i in {1..101}; do
curl http://localhost:9000/v1/endpoint
done
# Check rate limit headers
curl -i http://localhost:9000/v1/endpointResponse Headers:
RateLimit-Limit: 100
RateLimit-Remaining: 99
RateLimit-Reset: 900
Rate Limit Exceeded (429):
{
"success": false,
"message": "Too many requests from this IP, please try again later."
}Edit middleware/rate-limiter.js:
const customLimiter = createLimiter({
windowMs: 10 * 60 * 1000, // 10 minutes
max: 50, // 50 requests
message: { success: false, message: "Custom message" },
prefix: "rl:custom:",
});const userLimiter = createLimiter({
windowMs: 15 * 60 * 1000,
max: 200,
keyGenerator: req => req.user?.id || req.ip,
});Edit bin/www:
const cpu = require("os").cpus().length;
for (let i = 0; i < cpu; i++) {
// Use all CPU cores
cluster.fork();
}sys_tables- Dynamic table configurationsys_dropdown_list- Dropdown optionssys_otp_template- OTP email templatescasbin_rule- RBAC policies
β
Always use transactions for multi-step database operations
β
Apply appropriate rate limiters to different endpoint types
β
Validate all inputs using Joi schemas
β
Use Casbin dispatcher for permission checks
β
Log important events with proper context
β
Handle errors centrally using error middleware
β
Use environment variables for configuration
β
Version your APIs for backward compatibility
β
Test thoroughly before production deployment
This is an internal template maintained by Codebucket Solutions. For issues or suggestions, please contact the development team.
ISC License - Copyright Β© Codebucket Solutions Pvt. Ltd.
# Check MySQL is running
mysql.server status
# Verify credentials in .env
DB_HOST=127.0.0.1
DB_USER=root
DB_PASSWORD=your_password# Check Redis is running
redis-cli ping # Should return PONG
# System will fallback to memory store with warning- Verify middleware is imported in
app.js - Check environment configuration
- Review console logs for initialization messages
# Find process using port 9000
lsof -ti:9000 | xargs kill -9
# Or change PORT in .env
PORT=9001For internal support, contact: team@thecodebucket.com
Repository: https://github.com/Codebucket-Solutions/node-template
This template now includes a root WORKFLOW.md, scripts/symphony-bootstrap.sh, and docs/SYMPHONY_SETUP.md so it can be used with OpenAI Symphony in addition to Codex worktree workflows.