This repository provides a basic structure for building REST APIs using Express and TypeScript. It includes essential configurations and dependencies required to help you get started quickly and securely.
- Express server setup with TypeScript
- Basic middleware configuration (CORS, JSON parsing, global error handler)
- Zod for object validation (DTOs, etc.)
- Support for custom API errors, including validation errors that are easily instantiated using errors thrown by Zod
- Pre-configured ESLint and Prettier for code quality and formatting
- Commit hooks that run ESLint and Prettier, configured using the Husky library
- Environment management with
dotenv
andconfig/env.ts
, which validates each environment variable - Testing with
jest
- Basic folder structure for organizing and versioning your API
- Node.js (v18 or higher)
- pnpm (of course you can use whichever package manager you want, but pnpm is highly recommended)
- Clone the repository:
git clone https://github.com/caiohperlin/rest-api-express-template.git
cd rest-api-express-template
- Install dependencies:
pnpm install
- Create a
.env
file in the root directory and add your environment variables:
PORT=5000
NODE_ENV=development
pnpm dev
- Start the development server with hot reload usingtsx
- Watches for file changes and automatically restarts the server
- Runs directly from TypeScript source files
-
pnpm start
- Start the production server- Runs the compiled JavaScript files from the
dist
directory - Make sure to run
pnpm build
first
- Runs the compiled JavaScript files from the
-
pnpm build
- Build the project for production- Compiles TypeScript to JavaScript using
tsc
- Resolves path aliases using
tsc-alias
- Output is generated in the
dist
directory
- Compiles TypeScript to JavaScript using
-
pnpm typecheck
- Run TypeScript type checking without emitting files- Useful for CI/CD pipelines or quick type validation
- Faster than full compilation
-
pnpm lint
- Run ESLint on the source code- Checks for code quality issues and potential bugs
- Covers
.ts
and.js
files in thesrc
directory
-
pnpm lint:fix
- Run ESLint and automatically fix issues- Same as
lint
but attempts to fix auto-fixable problems
- Same as
-
pnpm format
- Format all files using Prettier- Applies consistent code formatting across the project
- Modifies files in place
-
pnpm format:check
- Check if files are properly formatted- Useful for CI/CD to ensure code is formatted
- Exits with error code if formatting is needed
-
pnpm test
- Run all tests using Jest- Includes
--passWithNoTests
flag for projects without tests yet - Runs once and exits
- Includes
-
pnpm test:watch
- Run tests in watch mode- Automatically re-runs tests when files change
- Interactive mode for development
-
pnpm test:coverage
- Run tests with coverage report- Generates detailed coverage reports
- Useful for understanding test coverage metrics
pnpm husky:prepare
- Initialize Husky git hooks- Sets up pre-commit hooks
- Automatically runs during
pnpm install
- Start development:
pnpm dev
- Make changes: Edit files in the
src
directory - Run tests:
pnpm test
orpnpm test:watch
- Check code quality:
pnpm lint
andpnpm format:check
- Commit changes: Git hooks will automatically run linting and formatting
- Push changes
- Build the project:
pnpm build
- Start production server:
pnpm start
- Environment variables: Ensure all required environment variables are set in your production environment
rest-api-express-template/
├── .husky/ # Git hooks configuration
├── dist/ # Compiled files (generated)
├── node_modules/ # Node.js modules
├── src/ # Source files
│ ├── api/ # API related files
│ │ └── v1/ # Version 1 of the API
│ │ ├── app/ # Example app module
│ │ └── index.ts # v1 Router
│ ├── config/ # Configuration files
│ │ └── env.ts # Environment variables validation
│ ├── errors/ # Custom error classes
│ ├── middleware/ # Custom middleware
│ ├── utils/ # Utility functions
│ ├── app.ts # Express application config
│ └── server.ts # Project entry point
├── .env # Environment variables
├── .env.example # Environment variables template
├── .lintstagedrc.json # lint-staged configuration
├── eslint.config.mjs # ESLint configuration
├── jest.config.js # Jest testing configuration
├── package.json # Project dependencies and scripts
├── pnpm-lock.yaml # pnpm lock file
├── prettier.config.js # Prettier configuration
├── README.md # Project documentation
└── tsconfig.json # TypeScript configuration
This project uses Husky to manage Git hooks:
- Pre-commit: Runs ESLint and Prettier on staged files only
Feel free to fork this repository and make your own changes. Pull requests are welcome!
This project is licensed under the ISC License.
Thank you for reading and happy express-ing! 👾