- Bun (v1.0 or higher)
-
Clone the repository:
git clone api-bun-typescript-template cd bun-ts-api -
Install dependencies:
bun install
-
Set up environment variables: Create a
.envfile in the root of the project and add the following variables:PORT=8080
To run the server in development mode with auto-reloading:
bun run devThe server will be available at http://localhost:8080.
To build and run the server in production mode:
-
Build the project:
bun run build
-
Start the server:
bun run start
In the package.json file, you'll find several scripts for common development tasks:
dev: Starts the development server with hot-reloading.start: Starts the application directly usingbun. For production, it's recommended tobuildfirst as described in the "Production" section.build: Compiles the TypeScript project into JavaScript for production.test: Runs both unit and integration tests.test:unit: Runs only the unit tests.test:integration: Runs only the integration tests.lint: Lints and automatically fixes issues in the codebase.lint:check: Checks for linting and formatting errors without applying changes.lint:check:fix: Checks for linting and formatting errors applying safe changes.format: Formats the entire codebase using Biome.ci:check: Runs all Biome checks (lint, format, etc.), suitable for a CI pipeline.
You can run any script using bun run <script-name>. For example:
bun run testYou can also run the application using Docker and Docker Compose.
-
Build and run with Docker Compose (recommended):
docker-compose up -d
This command builds the image and starts the container in the background. The API will be available at
http://localhost:8080. Thedb.jsonfile is mounted as a volume to persist data across container restarts. -
Manual build and run with Docker:
- Build the image:
docker build -t bun-ts-api . - Run the container:
docker run -p 8080:8080 -v ./db.json:/app/db.json --env-file ./.env bun-ts-api
- Build the image:
This project uses Swagger for API documentation. Once the server is running, you can access the interactive API documentation by navigating to /api-docs in your browser.
- URL:
http://localhost:8080/api-docs
The documentation provides detailed information about all available endpoints, including request parameters, response bodies, and status codes.
All endpoints are prefixed with /api.
POST /api/users: Create a new user.- Body:
{ "name": "John Doe", "email": "john.doe@example.com", "password": "yourpassword" }
- Body:
The project follows a modular, layered architecture:
src/: Main source code directory.adapters/: Connectors to external systems (e.g., database).config/: Application configuration.middleware/: Express middlewares.modules/: Feature-based modules (e.g.,user,account,card). Each module contains its own router, controller, services, schemas, and types.routes.ts: Main API router that aggregates all module routers.app.ts: Express application setup.index.ts: Application entry point.