https://github.com/easydevv/cloudflare-hono-vitest
A simple Hono application demonstrating deployment on Cloudflare Workers, complete with tests using Vitest. Exposes an endpoint returning environment variables in JSON format.
https://github.com/easydevv/cloudflare-hono-vitest
api cloudflare-workers hono javascript serverless typescript vitest
Last synced: about 1 year ago
JSON representation
A simple Hono application demonstrating deployment on Cloudflare Workers, complete with tests using Vitest. Exposes an endpoint returning environment variables in JSON format.
- Host: GitHub
- URL: https://github.com/easydevv/cloudflare-hono-vitest
- Owner: EasyDevv
- Created: 2025-01-13T06:21:11.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-18T09:00:24.000Z (about 1 year ago)
- Last Synced: 2025-03-03T04:13:19.935Z (about 1 year ago)
- Topics: api, cloudflare-workers, hono, javascript, serverless, typescript, vitest
- Language: TypeScript
- Homepage:
- Size: 51.8 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Cloudflare Workers Hono Vitest Template
Build and Test Serverless Apps Quickly.
This template provides an example of testing a Hono app running on Cloudflare Workers using Vitest. It includes a simple example code that outputs environment variables as JSON, and demonstrates how to manage important API keys for local development.
## Prerequisites
Before getting started, ensure you have the following:
- [Bun](https://bun.sh/)
- [Cloudflare Account](https://dash.cloudflare.com/)
## Project Structure
- `src/index.ts`: Main Hono file.
- `test/index.test.ts`: Vitest test cases.
- `.dev.vars`: File to define environment variables and secrets for local development.
- `wrangler.toml`: Cloudflare Workers configuration file.
## Setup
### 1. Create a New Project:
Replace `my-project` with your desired project name. If not provided, the project will be named `cloudflare-hono-vitest`.
```bash
bun create easydevv/cloudflare-hono-vitest my-project
# bun create `template` `project-name`
```
### 2. Configure `wrangler.toml`:
Here’s an example configuration for `wrangler.toml`:
```toml
name = "my-project"
main = "src/index.ts"
[vars]
MY_VARIABLE = "your_variable"
```
### 3. Set Up Environment Variables (Optional):
Create a `.dev.vars` file in the project root. This file is automatically loaded by Wrangler when running the application locally.
Example `.dev.vars`:
```plaintext
MY_SECRET="your_secret"
```
These variables can be accessed in the Hono application via `c.env`.
## Running the Application
To run the application locally using Wrangler:
```bash
bun run dev # or bun dev
```
This command starts a local server accessible at `http://localhost:8787`.
## Running Tests
To run tests using Bun:
```bash
bun run test
```
Alternatively, using bunx:
```bash
bunx vitest
```
## Production Setup
#### 1. Set Up SECRET in Cloudflare Workers:
Use the Wrangler CLI to set secrets for your application:
```bash
wrangler secret put MY_SECRET
```
You will be prompted to enter the value for `MY_SECRET`. Input your API key or other sensitive data.
#### 2. Deploy the Application:
Deploy the Hono application to Cloudflare Workers using the following command:
```bash
bun run deploy
```
After deployment, you can monitor it in the Cloudflare dashboard:
- [Cloudflare Dashboard](https://dash.cloudflare.com/)
## Conclusion
For more information on Hono, Cloudflare Workers, and Vitest, refer to their official documentation:
- [Hono Documentation](https://hono.dev/docs/getting-started/cloudflare-workers)
- [Cloudflare Workers Documentation](https://developers.cloudflare.com/workers/)
- [Vitest Documentation](https://vitest.dev/guide/)