Skip to content

Commit 12df223

Browse files
yusukebeusualoma
andauthored
feat: introduce serve command (npm#6)
* feat: introduce `serve` command Co-authored-by: Taku Amano <taku@taaas.jp> * formart * fix the defulat port as 7070 * add test * add `poweredBy` and update CLAUDE.md * don't use default file name * update the readme * make `utils/build.ts` * define func and functions* outside the handler * fix the import path of `getConnInfo` --------- Co-authored-by: Taku Amano <taku@taaas.jp>
1 parent 1e0267c commit 12df223

14 files changed

Lines changed: 868 additions & 158 deletions

File tree

CLAUDE.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ For efficient documentation access from web URLs, convert paths using the follow
8989

9090
Simply remove the `https://hono.dev` portion and append the path to the `hono docs` command to access documentation directly in the terminal.
9191

92+
**Important**: When you need to fetch information from Hono documentation (e.g., `https://hono.dev/docs/middleware/builtin/basic-auth`), always use the `hono docs` command instead of WebFetch. For example:
93+
94+
- Instead of: `WebFetch(https://hono.dev/docs/middleware/builtin/basic-auth)`
95+
- Use: `hono docs /docs/middleware/builtin/basic-auth`
96+
9297
### Recommended Workflow
9398

9499
1. **Search first**: Use `hono search <keyword>` to find relevant documentation

README.md

Lines changed: 113 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -14,80 +14,25 @@ npm install -g @hono/cli
1414
# Show help
1515
hono --help
1616

17-
# Send request to Hono app
18-
hono request
19-
2017
# Display documentation
2118
hono docs
2219

2320
# Search documentation
2421
hono search middleware
25-
```
26-
27-
## Commands
28-
29-
- `request [file]` - Send request to Hono app using `app.request()`
30-
- `docs [path]` - Display Hono documentation
31-
- `search <query>` - Search Hono documentation
32-
33-
### `request`
34-
35-
Send HTTP requests to your Hono application using the built-in `app.request()` method. This is particularly useful for testing and development.
36-
37-
```bash
38-
hono request [file] [options]
39-
```
40-
41-
**Arguments:**
4222

43-
- `file` - Path to the Hono app file (TypeScript/JSX supported, optional)
44-
45-
**Options:**
46-
47-
- `-P, --path <path>` - Request path (default: "/")
48-
- `-X, --method <method>` - HTTP method (default: GET)
49-
- `-d, --data <data>` - Request body data
50-
- `-H, --header <header>` - Custom headers (can be used multiple times)
51-
52-
**Examples:**
53-
54-
```bash
55-
# GET request to default app root (uses src/index.ts or src/index.tsx)
23+
# Send request to Hono app
5624
hono request
5725

58-
# GET request to specific path
59-
hono request -P /users/123
60-
61-
# POST request with data
62-
hono request -P /api/users -X POST -d '{"name":"Alice"}'
63-
64-
# Request to specific file
65-
hono request -P /api src/your-app.ts
66-
67-
# Request with custom headers
68-
hono request -P /api/protected -H 'Authorization: Bearer token' -H 'User-Agent: MyApp' src/your-app.ts
69-
70-
# PUT request with data and headers to specific file
71-
hono request -P /api/users/1 -X PUT -d '{"name":"Bob"}' -H 'Content-Type: application/json' src/your-app.ts
72-
73-
# Complex example with multiple options
74-
hono request -P /webhook -X POST -d '{"event":"test"}' -H 'Content-Type: application/json' -H 'X-API-Key: secret' my-project/server.ts
26+
# Start server
27+
hono serve
7528
```
7629

77-
**Response Format:**
78-
79-
The command returns a JSON object with the following structure:
30+
## Commands
8031

81-
```json
82-
{
83-
"status": 200,
84-
"body": "{\"message\":\"Hello World\"}",
85-
"headers": {
86-
"content-type": "application/json",
87-
"x-custom-header": "value"
88-
}
89-
}
90-
```
32+
- `docs [path]` - Display Hono documentation
33+
- `search [query]` - Search Hono documentation
34+
- `request [file]` - Send request to Hono app using `app.request()`
35+
- `serve [entry]` - Start server for Hono app
9136

9237
### `docs`
9338

@@ -175,6 +120,111 @@ Example output:
175120
Command: hono docs /docs/middleware/third-party
176121
```
177122

123+
### `request`
124+
125+
Send HTTP requests to your Hono application using the built-in `app.request()` method. This is particularly useful for testing and development.
126+
127+
```bash
128+
hono request [file] [options]
129+
```
130+
131+
**Arguments:**
132+
133+
- `file` - Path to the Hono app file (TypeScript/JSX supported, optional)
134+
135+
**Options:**
136+
137+
- `-P, --path <path>` - Request path (default: "/")
138+
- `-X, --method <method>` - HTTP method (default: GET)
139+
- `-d, --data <data>` - Request body data
140+
- `-H, --header <header>` - Custom headers (can be used multiple times)
141+
142+
**Examples:**
143+
144+
```bash
145+
# GET request to default app root (uses src/index.ts or src/index.tsx)
146+
hono request
147+
148+
# GET request to specific path
149+
hono request -P /users/123
150+
151+
# POST request with data
152+
hono request -P /api/users -X POST -d '{"name":"Alice"}'
153+
154+
# Request to specific file
155+
hono request -P /api src/your-app.ts
156+
157+
# Request with custom headers
158+
hono request -P /api/protected -H 'Authorization: Bearer token' -H 'User-Agent: MyApp' src/your-app.ts
159+
160+
# PUT request with data and headers to specific file
161+
hono request -P /api/users/1 -X PUT -d '{"name":"Bob"}' -H 'Content-Type: application/json' src/your-app.ts
162+
163+
# Complex example with multiple options
164+
hono request -P /webhook -X POST -d '{"event":"test"}' -H 'Content-Type: application/json' -H 'X-API-Key: secret' my-project/server.ts
165+
```
166+
167+
**Response Format:**
168+
169+
The command returns a JSON object with the following structure:
170+
171+
```json
172+
{
173+
"status": 200,
174+
"body": "{\"message\":\"Hello World\"}",
175+
"headers": {
176+
"content-type": "application/json",
177+
"x-custom-header": "value"
178+
}
179+
}
180+
```
181+
182+
### `serve`
183+
184+
Start a server for your Hono application. This is a simple server specialized for Hono applications with built-in TypeScript and JSX support.
185+
186+
```bash
187+
hono serve [entry] [options]
188+
```
189+
190+
**Arguments:**
191+
192+
- `entry` - Entry file for your Hono app (TypeScript/JSX supported, optional)
193+
194+
**Options:**
195+
196+
- `-p, --port <port>` - Port number (default: 7070)
197+
- `--show-routes` - Show registered routes
198+
- `--use <middleware>` - Use middleware (can be used multiple times)
199+
200+
**Examples:**
201+
202+
```bash
203+
# Start server with default empty app (no entry file needed)
204+
hono serve
205+
206+
# Start server on specific port
207+
hono serve -p 8080 src/app.ts
208+
209+
# Start server with specific entry file
210+
hono serve src/app.ts
211+
212+
# Start server and show routes
213+
hono serve --show-routes src/app.ts
214+
215+
# Start server with middleware
216+
hono serve --use 'cors()' src/app.ts
217+
218+
# Start server with multiple middleware
219+
hono serve --use 'cors()' --use 'logger()' src/app.ts
220+
221+
# Start server with authentication and static file serving
222+
hono serve --use 'basicAuth({username:"foo", password:"bar"})' --use "serveStatic({ root: './' })"
223+
224+
# Combine all options
225+
hono serve -p 8080 --show-routes --use 'cors()' --use 'logger()' src/app.ts
226+
```
227+
178228
## Authors
179229

180230
- Yusuke Wada https://github.com/yusukebe

0 commit comments

Comments
 (0)