
In today’s microservices-driven development landscape, setting up new services with consistent architecture, proper tooling, and best practices can be time-consuming and error-prone. What if you could generate a complete Node.js microservice template with HTTP endpoints, Kafka messaging, Redis caching, and Docker configuration in just a few seconds?
In this 4-part series, we’ll build a professional CLI tool called create-node-microservice that generates production-ready Node.js microservice templates. By the end of this series, you’ll have a powerful tool that can scaffold complete microservices with modern architecture patterns.
Why Build a CLI Generator?
CLI generators solve several critical problems in modern development:
- Consistency: Ensures all projects follow the same architecture and conventions
- Speed: Reduces project setup time from hours to minutes
- Best Practices: Bakes in security, performance, and maintainability patterns
- Onboarding: New team members can start productive work immediately
What We’ll Build
Our CLI tool will generate Node.js microservices with these features:
Core Services
- HTTP API with Express.js
- Kafka producer/consumer
- Redis client integration
- Health check endpoints
Development Features
- TypeScript support
- Environment configuration
- Docker containerization
- Graceful shutdown handling
Architecture Overview
Our generated microservices will follow this architecture pattern:
flowchart TD A[HTTP Client] -->|REST API| B[Express Server] B --> C[Business Logic] C --> D[Kafka Producer] C --> E[Redis Cache] F[Kafka Consumer] --> C G[External Service] -->|Messages| F subgraph "Generated Microservice" B C D E F end style A fill:#e1f5fe style B fill:#f3e5f5 style C fill:#e8f5e8 style D fill:#fff3e0 style E fill:#ffebee style F fill:#fff3e0 style G fill:#e1f5fe
Project Structure Overview
The CLI tool itself will have this structure:
create-node-microservice/
├── bin/
│ └── cli.js # Entry point
├── src/
│ ├── cli.ts # Main CLI logic
│ ├── prompts.ts # Interactive prompts
│ ├── generator.ts # Template generator
│ └── templates/ # Mustache templates
│ ├── package.json.mustache
│ ├── src/
│ │ ├── index.ts.mustache
│ │ ├── services/
│ │ └── routes/
│ ├── docker-compose.yml.mustache
│ └── .env.example.mustache
├── package.json
└── README.md
Setting Up the Development Environment
Let’s start by creating our CLI project structure:
# Create project directory
mkdir create-node-microservice
cd create-node-microservice
# Initialize package.json
npm init -y
# Install dependencies
npm install commander inquirer chalk ora fs-extra mustache validate-npm-package-name
# Install dev dependencies
npm install -D typescript @types/node @types/inquirer @types/fs-extra @types/mustache tsx jest
Package.json Configuration
Update your package.json
with the essential configuration:
{
"name": "create-node-microservice",
"version": "1.0.0",
"description": "CLI tool to generate Node.js microservice templates",
"bin": {
"create-node-microservice": "./bin/cli.js"
},
"scripts": {
"build": "tsc",
"dev": "tsx watch src/cli.ts",
"test": "jest",
"prepublishOnly": "npm run build"
},
"keywords": ["cli", "template", "generator", "microservice"],
"files": ["bin", "dist", "templates"],
"engines": {
"node": ">=18.0.0"
}
}
TypeScript Configuration
Create a tsconfig.json
for TypeScript compilation:
{
"compilerOptions": {
"target": "ES2022",
"module": "ESNext",
"moduleResolution": "node",
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"declaration": true,
"declarationMap": true,
"sourceMap": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist", "templates"]
}
What’s Next?
In Part 2, we’ll dive into building the interactive CLI interface using Commander.js and Inquirer.js. We’ll create beautiful prompts that guide users through configuring their microservice template, including service selection, ports, and database connections.
Key topics we’ll cover in the next post:
- Building interactive CLI prompts
- Input validation and error handling
- Configuration management
- Beautiful terminal UI with colors and spinners
Make sure to follow along as we build this powerful development tool that will streamline your microservice development workflow!
Have questions or suggestions? Drop them in the comments below!