Skip to content
Myr

Documentation (#24)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Create context.ts and test * Handling TypeError * Implemented c.json() * Update readme and example * Update sample code * Update readme * Write documents

authored by Yusuke Wada · committed by GitHub · parent ee80ae3 · Browse files · 1 file+69 added−24 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+69 added−24 removed

  • other:+69 added−24 removed

1 file+69 added−24 removed

modified:README.md+69 added-24 removed
4 unmodified lines
5```js6const { Hono } = require('hono')7const app = new Hono()89app.get('/', () => new Response('Hono!!'))9app.get('/', (c) => c.text('Hono!!'))1011app.fire()12```1314![carbon](https://user-images.githubusercontent.com/10682/147877725-bce9bd46-953d-4d70-9c2b-3eae47ad4df9.png)14Hono[炎] - _**means flame🔥 in Japanese**_ - is small, fast and simple web flamework for a Service Workers API based serverless service such as **Cloudflare Workers** and **Fastly Compute@Edge**. Because Hono does not depend on any NPM packages, it's easy to install, setup, and deploy. Although Hono has a router, context object, and middleware including builtin. Mostly web application like a Web API can be made.1516In the case of Cloudflare Workers, there are `wrangler` and `miniflare`.You can develop the application locally and publish it with few commands.1718It's amazing that the experience of writing code with Hono. Enjoy!1920## Feature2118- Fast - the router is implemented with Trie-Tree structure.19- Portable - zero dependencies.20- Flexible - you can make your own middlewares.21- Easy - simple API, builtin middleware, and TypeScript support.22- Optimized - for Cloudflare Workers or Fastly Compute@Edge.22- **Fast** - the router is implemented with Trie-Tree structure.23- **Tiny** - zero dependencies, using Web standard API.24- **Flexible** - you can make your own middleware.25- **Easy** - simple API, builtin middleware, and written in TypeScript.26- **Optimized** - for Cloudflare Workers or Fastly Compute@Edge.2728## Benchmark2926Hono is fastest!!30**Hono is fastest** compared to other routers for Cloudflare Workers.3132```33hono x 758,264 ops/sec ±5.41% (75 runs sampled)34itty-router x 158,359 ops/sec ±3.21% (89 runs sampled)
1 unmodified line
36Fastest is hono37✨ Done in 42.84s.38```3940## Hono in 1 minute4142Below is demonstration to create an application of Cloudflare Workers with Hono.4344![Demo](https://user-images.githubusercontent.com/10682/148223268-2484a891-57c1-472f-9df3-936a5586f002.gif)4546## Install4748You can install from npm public registry:4950```51$ yarn add hono52```53
4 unmodified lines
58```5960## Methods6162Instance of `Hono` has these methods:6364- app.**HTTP_METHOD**(path, handler)65- app.**all**(path, handler)66- app.**route**(path)67- app.**use**(path, middleware)
49 unmodified lines
117 .post(() => {...})118 .put(() => {...})119```120107## Async121## async/await122123```js124app.get('/fetch-url', async () => {125 const response = await fetch('https://example.com/')
11 unmodified lines
137...138139app.use('*', Middleware.poweredBy())140app.use('*', Middleware.logger())127141```142143Available builtin middleware are listed on [src/middleware](https://github.com/yusukebe/hono/tree/master/src/middleware) directory.144145### Custom Middleware146147You can write your own middleware:148149```js150// Custom logger151app.use('*', async (c, next) => {152 console.log(`[${c.req.method}] ${c.req.url}`)
10 unmodified lines
163```164165### Custom 404 Response166167If you want to customize 404 Not Found response:168169```js170app.use('*', async (c, next) => {171 await next()172 if (c.res.status === 404) {
3 unmodified lines
176```177178### Complex Pattern179180You can also do this:181182```js183// Output response time184app.use('*', async (c, next) => {185 await next()
11 unmodified lines
197```198199## Context200180### req201To handle Request and Reponse easily, you can use Context object:202203### c.req204205```js206207// Get Request object208app.get('/hello', (c) => {
13 unmodified lines
222 ...223})224```225203### res226### c.res227228```js229// Response object230app.use('/', (c, next) => {
1 unmodified line
232 c.res.headers.append('X-Debug', 'Debug message')233})234```235213### text236### c.text()237238Render text as `Content-Type:text/plain`:239240```js241app.get('/say', (c) => {242 return c.text('Hello!')243})244```245221### json246### c.json()247248Render text as `Content-Type:application/json`:249250```js251app.get('/api', (c) => {252 return c.json({ message: 'Hello!' })253})254```255229## Hono in 1 minute256## Cloudflare Workers with Hono257231Create your first Cloudflare Workers with Hono from scratch.258Write your first code for Cloudflare Workers with Hono.259233![Demo](https://user-images.githubusercontent.com/10682/148223268-2484a891-57c1-472f-9df3-936a5586f002.gif)234260### 1. Install Wrangler261262Install Cloudflare Command Line "[Wrangler](https://github.com/cloudflare/wrangler)"263
20 unmodified lines
284```285286### 4. `npm install hono`287263Install `hono` from npm repository.288Install `hono` from npm registry.289290```291$ npm i hono292```
5 unmodified lines
298```js299const { Hono } = require('hono')300const app = new Hono()301277app.get('/', () => new Response('Hello! Hono!'))302app.get('/', (c) => c.text('Hello! Hono!'))303304app.fire()305```306282### 6. Run!307### 6. Run308309Run the development server locally.310311```sh312$ wrangler dev313```314315### Publish316317Deploy to Cloudflare. That's all!318319```sh320$ wrangler publish321```322323## Related projects324292- koa <https://github.com/koajs/koa>325Implementation of the router is inspired by [goblin](https://github.com/bmf-san/goblin). API design is inspired by [express](https://github.com/expressjs/express) and [koa](https://github.com/koajs/koa). [itty-router](https://github.com/kwhitley/itty-router) and [Sunder](https://github.com/SunderJS/sunder) are the other routers or frameworks for Cloudflare Workers.326327- express <https://github.com/expressjs/express>294- oak <https://github.com/oakserver/oak>328- koa <https://github.com/koajs/koa>329- itty-router <https://github.com/kwhitley/itty-router>330- Sunder <https://github.com/SunderJS/sunder>331- goblin <https://github.com/bmf-san/goblin>332333## Contributing334335Contributions Welcome! You can contribute by the following way:336337- Write or fix documents338- Write code of middleware339- Fix bugs340- Refactor the code341342If you can, please!343344## Author345346Yusuke Wada <https://github.com/yusukebe>347348## License349305MIT350Distributed under the MIT License. See [LICENSE](LISENCE) for more information.