Skip to content

Commit 89e91ac

Browse files
authored
Add missing serve-static example and update README (#226)
* Add missing serve-static example and update README * Add space before closing bracket * Remove irrelevant test file * Remove irrelevant test file and test command
1 parent d622d6f commit 89e91ac

File tree

8 files changed

+86
-1
lines changed

8 files changed

+86
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ This repository contains examples that use [Hono](https://hono.dev).
1010
- [env-vars](./env-vars/) - example using environment variables with Hono in Typescript: `Bindings` & `Variables`
1111
- [jsx-ssr](./jsx-ssr/) - JSX Server Side Rendering with `JSX` Middleware
1212
- [hono-vite-jsx](./hono-vite-jsx/) - Example of using `hono/jsx/dom` with `vite`
13-
- [serve-static](./serve-static/) - example of `Serve Static` Middleware
13+
- [serve-static](./serve-static/) - example of serving static files using Cloudflare Workers
1414
- [deno](./deno/) - Deno example
1515
- [bun](./bun/) - Bun example
1616
- [pages-stack](./pages-stack/) - Zod + Zod Validator + `hc` + React on Cloudflare Pages

serve-static/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Hono static file example for Cloudflare Workers
2+
3+
This example demonstrates how to serve static files using Cloudflare Workers. Please learn more in the [Hono documentation](https://hono.dev/docs/getting-started/cloudflare-workers#serve-static-files) and [Cloudflare Workers documentation](https://developers.cloudflare.com/workers/static-assets/binding/#directory).
4+
5+
Before you can start publishing your service to cloudflare worker, you must sign up for a Cloudflare Workers account first, you can check out this [document](https://developers.cloudflare.com/workers/get-started/guide)
6+
7+
You can update the information (`name`, `zone_id`, etc) in wrangler file, then you can test and deploy your service by simply doing,
8+
9+
```txt
10+
npm install
11+
npm run dev # Start a local server for developing your worker
12+
npm run deploy # Publish your worker to the orange cloud
13+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a nested sample file

serve-static/assets/my-file.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a sample file

serve-static/package.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "serve-static",
3+
"version": "0.0.1",
4+
"private": true,
5+
"type": "module",
6+
"scripts": {
7+
"dev": "wrangler dev src/index.ts",
8+
"deploy": "wrangler deploy src/index.ts"
9+
},
10+
"license": "MIT",
11+
"dependencies": {
12+
"hono": "^4.2.4"
13+
},
14+
"devDependencies": {
15+
"@cloudflare/workers-types": "^4.20240405.0",
16+
"vitest": "^1.5.0",
17+
"wrangler": "^3.53.0"
18+
}
19+
}

serve-static/src/index.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Hono } from "hono";
2+
import { poweredBy } from "hono/powered-by";
3+
4+
const app = new Hono();
5+
6+
// Mount Builtin Middleware
7+
app.use("*", poweredBy());
8+
9+
// Routing
10+
app.get("/", (c) =>
11+
c.html(
12+
`<html lang="en">
13+
<head>
14+
<title>Hono</title>
15+
</head>
16+
<body>
17+
<h1>Welcome to Hono</h1>
18+
<p>
19+
Try visiting: <a href="/my-file.txt">/my-file.txt</a> and <a href="/folder/nested-file.txt">/folder/nested-file.txt</a>
20+
</p>
21+
<p>
22+
Learn more about serving static files in Hono <a target="_blank" href="https://hono.dev/docs/getting-started/cloudflare-workers#serve-static-files">here</a>
23+
</p>
24+
</body>
25+
</html>`
26+
)
27+
);
28+
29+
export default app;

serve-static/tsconfig.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ESNext",
4+
"module": "ESNext",
5+
"moduleResolution": "Bundler",
6+
"esModuleInterop": true,
7+
"strict": true,
8+
"lib": [
9+
"ESNext"
10+
],
11+
"types": [
12+
"vitest/globals",
13+
"@cloudflare/workers-types"
14+
],
15+
"jsx": "react-jsx",
16+
"jsxImportSource": "hono/jsx"
17+
},
18+
}

serve-static/wrangler.toml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name = "hono-example-basic"
2+
compatibility_date = "2024-09-19"
3+
4+
assets = { directory = "./assets/" }

0 commit comments

Comments
 (0)