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
Provenance
- Commit
2d0e7ee- Tree
f21d401- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
ee80ae3
1 file changed+69 added−24 removed
modified:README.md+69 added-24 removed
4 unmodified lines5 ```js6 const { Hono } = require('hono')7 const app = new Hono()8 9-app.get('/', () => new Response('Hono!!'))9+app.get('/', (c) => c.text('Hono!!'))10 11 app.fire()12 ```13 14-14+Hono[炎] - _**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.15 16+In the case of Cloudflare Workers, there are `wrangler` and `miniflare`.You can develop the application locally and publish it with few commands.17+18+It's amazing that the experience of writing code with Hono. Enjoy!19+20 ## Feature21 18-- 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.27 28 ## Benchmark29 26-Hono is fastest!!30+**Hono is fastest** compared to other routers for Cloudflare Workers.31 32 ```33 hono x 758,264 ops/sec ±5.41% (75 runs sampled)34 itty-router x 158,359 ops/sec ±3.21% (89 runs sampled)1 unmodified line36 Fastest is hono37 ✨ Done in 42.84s.38 ```39 40+## Hono in 1 minute41+42+Below is demonstration to create an application of Cloudflare Workers with Hono.43+44+45+46 ## Install47 48+You can install from npm public registry:49+50 ```51 $ yarn add hono52 ```53 4 unmodified lines58 ```59 60 ## Methods61 62+Instance of `Hono` has these methods:63+64 - app.**HTTP_METHOD**(path, handler)65 - app.**all**(path, handler)66 - app.**route**(path)67 - app.**use**(path, middleware)49 unmodified lines117 .post(() => {...})118 .put(() => {...})119 ```120 107-## Async121+## async/await122 123 ```js124 app.get('/fetch-url', async () => {125 const response = await fetch('https://example.com/')11 unmodified lines137 ...138 139 app.use('*', Middleware.poweredBy())140 app.use('*', Middleware.logger())127-141 ```142 143+Available builtin middleware are listed on [src/middleware](https://github.com/yusukebe/hono/tree/master/src/middleware) directory.144+145 ### Custom Middleware146 147+You can write your own middleware:148+149 ```js150 // Custom logger151 app.use('*', async (c, next) => {152 console.log(`[${c.req.method}] ${c.req.url}`)10 unmodified lines163 ```164 165 ### Custom 404 Response166 167+If you want to customize 404 Not Found response:168+169 ```js170 app.use('*', async (c, next) => {171 await next()172 if (c.res.status === 404) {3 unmodified lines176 ```177 178 ### Complex Pattern179 180+You can also do this:181+182 ```js183 // Output response time184 app.use('*', async (c, next) => {185 await next()11 unmodified lines197 ```198 199 ## Context200 180-### req201+To handle Request and Reponse easily, you can use Context object:202 203+### c.req204+205 ```js206 207 // Get Request object208 app.get('/hello', (c) => {13 unmodified lines222 ...223 })224 ```225 203-### res226+### c.res227 228 ```js229 // Response object230 app.use('/', (c, next) => {1 unmodified line232 c.res.headers.append('X-Debug', 'Debug message')233 })234 ```235 213-### text236+### c.text()237 238+Render text as `Content-Type:text/plain`:239+240 ```js241 app.get('/say', (c) => {242 return c.text('Hello!')243 })244 ```245 221-### json246+### c.json()247 248+Render text as `Content-Type:application/json`:249+250 ```js251 app.get('/api', (c) => {252 return c.json({ message: 'Hello!' })253 })254 ```255 229-## Hono in 1 minute256+## Cloudflare Workers with Hono257 231-Create your first Cloudflare Workers with Hono from scratch.258+Write your first code for Cloudflare Workers with Hono.259 233-234-260 ### 1. Install Wrangler261 262 Install Cloudflare Command Line "[Wrangler](https://github.com/cloudflare/wrangler)"263 20 unmodified lines284 ```285 286 ### 4. `npm install hono`287 263-Install `hono` from npm repository.288+Install `hono` from npm registry.289 290 ```291 $ npm i hono292 ```5 unmodified lines298 ```js299 const { Hono } = require('hono')300 const app = new Hono()301 277-app.get('/', () => new Response('Hello! Hono!'))302+app.get('/', (c) => c.text('Hello! Hono!'))303 304 app.fire()305 ```306 282-### 6. Run!307+### 6. Run308 309 Run the development server locally.310 311 ```sh312 $ wrangler dev313 ```314 315+### Publish316+317+Deploy to Cloudflare. That's all!318+319+```sh320+$ wrangler publish321+```322+323 ## Related projects324 292-- koa <https://github.com/koajs/koa>325+Implementation 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.326+327 - 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>332 333+## Contributing334+335+Contributions Welcome! You can contribute by the following way:336+337+- Write or fix documents338+- Write code of middleware339+- Fix bugs340+- Refactor the code341+342+If you can, please!343+344 ## Author345 346 Yusuke Wada <https://github.com/yusukebe>347 348 ## License349 305-MIT350+Distributed under the MIT License. See [LICENSE](LISENCE) for more information.