UNPKG

barnes-node-ts-express-starter

Version:

A modern Node.js TypeScript Express starter template with essential development tooling

179 lines (135 loc) • 5.17 kB
# Barnes Node.js TypeScript Express Starter A modern, production-ready Node.js backend starter template with TypeScript, Express, and essential development tooling. Get up and running in seconds with a well-structured, scalable backend foundation. ## šŸš€ Quick Start Create a new backend project instantly using npx: ```bash npx barnes-node-ts-express-starter ``` Follow the prompts to: 1. Enter your project name 2. Navigate to the created directory 3. Install dependencies 4. Start developing! ```bash cd your-project-name npm install npm run dev ``` Your server will be running at `http://localhost:3001` šŸŽ‰ ## ✨ Features - **TypeScript**: Full TypeScript support with strict configuration - **Express.js**: Fast, minimalist web framework - **Hot Reload**: Automatic server restart on file changes with nodemon - **Environment Management**: Flexible environment configuration - **Modern ES Modules**: Uses ES6 import/export syntax - **Health Check**: Built-in status endpoint for monitoring - **Production Ready**: Includes build scripts and optimized configuration - **Clean Architecture**: Well-organized project structure ## šŸ“ Project Structure ``` your-project/ ā”œā”€ā”€ src/ │ ā”œā”€ā”€ app.ts # Express app configuration │ ā”œā”€ā”€ server.ts # Server entry point │ ā”œā”€ā”€ config/ │ │ └── env.ts # Environment configuration │ └── routes/ │ ā”œā”€ā”€ index.ts # Main router │ └── status.ts # Health check endpoint ā”œā”€ā”€ dist/ # Compiled JavaScript (after build) ā”œā”€ā”€ nodemon.json # Nodemon configuration for development ā”œā”€ā”€ nodemon.local.json # Nodemon configuration for local env ā”œā”€ā”€ tsconfig.json # TypeScript configuration ā”œā”€ā”€ package.json # Dependencies and scripts └── README.md # This file ``` ## šŸ›  Available Scripts - **`npm run dev`** - Start development server with hot reload (NODE_ENV=development) - **`npm run dev:local`** - Start development server in local mode (loads .env file) - **`npm run build`** - Compile TypeScript to JavaScript - **`npm start`** - Start production server from compiled code ## šŸ”§ Environment Configuration The starter supports multiple environment modes: ### Development Mode ```bash npm run dev ``` - Uses `NODE_ENV=development` - No .env file loading (uses system environment variables) ### Local Mode ```bash npm run dev:local ``` - Uses `NODE_ENV=local` - Automatically loads `.env` file for local development - Create a `.env` file in your project root: ```env PORT=3001 APP_MESSAGE=Hello from local development! ``` ### Production ```bash npm run build npm start ``` - Runs compiled JavaScript from `dist/` folder - Uses production environment variables ## šŸ“” API Endpoints ### Health Check - **GET** `/status` - Returns server health information Example response: ```json { "uptime": 123.456, "message": "OK", "timestamp": 1674567890123 } ``` ## šŸš€ Getting Started with Development 1. **Add new routes**: Create new route files in `src/routes/` and register them in `src/routes/index.ts` 2. **Environment variables**: Add your variables to the `.env` file for local development 3. **Middleware**: Add Express middleware in `src/app.ts` 4. **Configuration**: Modify environment loading in `src/config/env.ts` ## šŸ“¦ Dependencies ### Production Dependencies - **express**: Fast, unopinionated web framework - **dotenv**: Loads environment variables from .env file ### Development Dependencies - **typescript**: TypeScript compiler - **@types/express**: TypeScript definitions for Express - **nodemon**: Monitors file changes and restarts server - **tsx**: TypeScript execution engine - **cross-env**: Cross-platform environment variable setting ## šŸ”„ Customization ### Adding Database Support ```bash # Example: Adding MongoDB with Mongoose npm install mongoose npm install -D @types/mongoose ``` ### Adding Authentication ```bash # Example: Adding JWT authentication npm install jsonwebtoken bcryptjs npm install -D @types/jsonwebtoken @types/bcryptjs ``` ### Adding Validation ```bash # Example: Adding request validation npm install joi npm install -D @types/joi ``` ## šŸ“‹ Best Practices Included - **TypeScript strict mode** for better code quality - **ES Modules** for modern JavaScript features - **Modular routing** for scalable code organization - **Environment-based configuration** for different deployment stages - **Health check endpoint** for monitoring and load balancers - **Development tooling** with hot reload and TypeScript watching ## šŸ¤ Contributing This starter template is designed to be a solid foundation for Node.js backend projects. Feel free to customize it according to your needs! ## šŸ“ License MIT License - feel free to use this starter for your projects! --- **Happy coding!** šŸŽÆ Built with ā¤ļø for rapid backend development