node-backend-template
v1.0.3
Published
A CLI tool to generate Node.js backend templates with Express and TypeScript.
Maintainers
Readme
node-backend-template
A minimal Node.js + TypeScript backend starter template and CLI tool using Express, Pino logging, validation middleware, 404 + error handling, and fast development cycle.
✅ Features
- Express server with
src/app.tsandsrc/server.ts - root endpoint:
GET /returns "Hello World!" - configurable via
.env(development/staging/production) - logging with
pino+rotating-file-stream - security middleware:
helmet,cors - centralized error handling + 404 not found
- utility module
sumwith Vitest unit tests - linting, formatting, commit hooks, and type-checking workflows
📦 Requirements
- Node.js 25.x (or latest compatible)
pnpm(project usespnpmpackage manager)
Usage with NPX
Quick start with npx (no installation required):
npx node-backend-template
pnpm dlx node-backend-templateRunning this starts an interactive project generator. It now supports:
- project name prompt (must be lowercase, URL-friendly)
- language selection:
TypeScriptorJavaScript - package manager selection:
npm,yarn, orpnpm
Once complete, it:
- creates the project folder
- copies the
templates/{ts|js}skeleton - renames
gitignore.template→.gitignore - updates
package.jsonname - copies
.env.exampleto.env
Example
npx node-backend-template
# Enter project name: my-app
# Choose language: TypeScript
# Choose package manager: pnpmThen run:
cd my-app
pnpm install
pnpm devThis command bootstraps a new backend project with all the starter template features.
Additional commands:
npx node-backend-template --help # Show all available commands
npx node-backend-template --version # Show version⚠️ Warning: Do NOT use
npm i node-backend-template. Always usenpx node-backend-templatefor the best experience with proper dependency management.
🛠️ Environment
Copy and set environment variables for your environment.
cp .env.example .env.development
cp .env.example .env.staging
cp .env.example .env.productionEnvironment Variables
PORT: The port number on which the Express server will listen (e.g.,3000)SERVER_URL: The base URL of the server (e.g.,http://localhost:3000)ENV: The environment mode, one ofdevelopment,staging, orproductionDATABASE_URL: The database connection string (optional; required if using a database)
▶️ Development
pnpm dev
pnpm dev:staging
pnpm dev:prodExpress server starts and logs via Pino. Root route is at GET /.
🧱 Build
pnpm buildBuild compiles with TypeScript using tsconfig.build.json.
🧪 Tests
pnpm test:run
pnpm test:ui
pnpm coverageIncludes sample unit test in src/__tests__/sum.spec.ts for src/utils/sum.ts.
🎯 Lint & formatter
pnpm lint
pnpm lint:fix
pnpm format
pnpm format:checkGit hooks via husky and lint-staged run lint + prettier before commit.
📁 Project structure
src/app.ts- Express app setupsrc/server.ts- server startup + graceful shutdownsrc/config/config.ts- env config loadersrc/middlewares/middlewares.ts- sample route handlersrc/middlewares/notFound.ts- 404 middlewaresrc/middlewares/globalErrorHandler.ts- error handlersrc/utils/logger.ts- pino logger config + file rotationsrc/utils/httpError.ts- http error wrappersrc/utils/errorObjects.ts- error response buildersrc/constant/*- response messages + environment enumssrc/type/types.ts- TS type definitionssrc/__tests__- unit tests
Middleware flow
pino-httplogs request/responseexpress.jsonparses incoming JSONcorsenables CORShelmetadds security headersexpress.staticservespublic/globalErrorHandlerhandles errors in JSON formatnotFoundhandles unknown routes
Contributing
Please see CONTRIBUTING.md for contribution guidelines and CODE_OF_CONDUCT.md for community expectations.
If you are contributing through a pull request, the basic flow is:
- Fork and branch
pnpm install- Add or update tests
- Run lint/format
- Open a pull request
🆘 Troubleshooting
- Port in use: change
PORTin.env - Type errors:
pnpm type-check - Commit blocked: run
pnpm lint:fixandpnpm format
