Skip to content
Myr

docs: update readme

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

authored by Yusuke Wada · committed by Yusuke Wada · parent bd29072 · Browse files · 1 file+69 added−58 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−58 removed

  • other:+69 added−58 removed

1 file+69 added−58 removed

modified:README.md+69 added-58 removed
1 unmodified line
23[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/yusukebe/hono/ci)](https://github.com/yusukebe/hono/actions)4[![GitHub](https://img.shields.io/github/license/yusukebe/hono)](https://github.com/yusukebe/hono/blob/master/LICENSE)5[![npm](https://img.shields.io/npm/v/hono)](https://www.npmjs.com/package/hono)6[![npm](https://img.shields.io/npm/dw/hono)](https://www.npmjs.com/package/hono)6[![npm](https://img.shields.io/npm/dm/hono)](https://www.npmjs.com/package/hono)7[![npm type definitions](https://img.shields.io/npm/types/hono)](https://www.npmjs.com/package/hono)8[![GitHub commit activity](https://img.shields.io/github/commit-activity/m/yusukebe/hono)](https://github.com/yusukebe/hono/pulse)9[![GitHub last commit](https://img.shields.io/github/last-commit/yusukebe/hono)](https://github.com/yusukebe/hono/commits/master)1011Hono[炎] - _**means flame🔥 in Japanese**_ - is small, simple, and ultrafast web framework for a Service Workers API based serverless such as Cloudflare Workers and Fastly Compute@Edge.11Hono[炎] - _**means flame🔥 in Japanese**_ - is small, simple, and ultrafast web framework for Service Worker based serverless applications like Cloudflare Workers and Fastly Compute@Edge.1213```js14import { Hono } from 'hono'15const app = new Hono()
4 unmodified lines
20```2122## Features2324- **Ultra fast** - the router is implemented with Trie-Tree structure. Not use loops.25- **Zero dependencies** - using only Web standard API.26- **Middleware** - builtin middleware, and you can make your own middleware.24- **Ultrafast** - the router does not use linear loops.25- **Zero-dependencies** - using only Web standard API.26- **Middleware** - builtin middleware and your own middleware.27- **Optimized** - for Cloudflare Workers.2829## Benchmark30
9 unmodified lines
40```4142## Hono in 1 minute4344Below is a demonstration to create an application of Cloudflare Workers with Hono.44A demonstration to create an application of Cloudflare Workers with Hono.4546![Demo](https://user-images.githubusercontent.com/10682/151973526-342644f9-71c5-4fee-81f4-64a7558bb192.gif)4748Now, the named path parameter has types.
1 unmodified line
50![Demo](https://user-images.githubusercontent.com/10682/154179671-9e491597-6778-44ac-a8e6-4483d7ad5393.png)5152## Install5354You can install Hono from npm registry:54You can install Hono from the npm registry.5556```sh57yarn add hono57$ yarn add hono58```5960or6162```sh63npm install hono63$ npm install hono64```6566## Methods6768An instance of `Hono` has these methods:68An instance of `Hono` has these methods.6970- app.**HTTP_METHOD**(path, handler)71- app.**all**(path, handler)72- app.**route**(path)
4 unmodified lines
77## Routing7879### Basic8081`app.HTTP_METHOD`8281```js82// HTTP Methods83app.get('/', (c) => c.text('GET /'))84app.post('/', (c) => c.text('POST /'))
1 unmodified line
86// Wildcard87app.get('/wild/*/card', (c) => {88 return c.text('GET /wild/*/card')89})92```9094`app.all`9596```js91// Any HTTP methods92app.all('/hello', (c) => c.text('Any Method /hello'))93```94
12 unmodified lines
107app.get('/post/:date{[0-9]+}/:title{[a-z]+}', (c) => {108 const date = c.req.param('date')109 const title = c.req.param('title')110 ...111})112```113114### Nested route115116```js117const book = app.route('/book')123book.get('/', (c) => c.text('List Books')) // => GET /book118book.get('/', (c) => c.text('List Books')) // GET /book119book.get('/:id', (c) => {125 // => GET /book/:id120 // GET /book/:id121 const id = c.req.param('id')122 return c.text('Get Book: ' + id)123})129book.post('/', (c) => c.text('Create Book')) // => POST /book124book.post('/', (c) => c.text('Create Book')) // POST /book125```126127### no strict128134If `strict` is set `false`, `/hello`and`/hello/` are treated the same:129If `strict` is set `false`, `/hello`and`/hello/` are treated the same. Default is `true`.130131```js132const app = new Hono({ strict: false })133134app.get('/hello', (c) => c.text('/hello or /hello/'))135```136142Default is `true`.143137### async/await138139```js140app.get('/fetch-url', async (c) => {
15 unmodified lines
156const app = new Hono()157158app.use('*', poweredBy())159app.use('*', logger())167app.use('/auth/*', basicAuth({ username: 'hono', password: 'acoolproject' }))160app.use(161 '/auth/*',162 basicAuth({163 username: 'hono',164 password: 'acoolproject',165 })166)167```168169Available builtin middleware are listed on [src/middleware](https://github.com/yusukebe/hono/tree/master/src/middleware).170171### Custom Middleware172174You can write your own middleware:173You can write your own middleware.174175```js176// Custom logger177app.use('*', async (c, next) => {
11 unmodified lines
189```190191## Context192194To handle Request and Reponse, you can use Context object:193To handle Request and Reponse, you can use Context object.194195### c.req196197```js
30 unmodified lines
228 c.header('Content-Type', 'text/plain')229 c.status(201)230231 return c.body('Thank you for comming')232})233```234234 /*235 Same as:236 return new Response('Thank you for comming', {237 status: 201,238 statusText: 'Created',239 headers: {240 'X-Message': 'Hello',241 'Content-Type': 'text/plain',242 'Content-Length: '22'243 }244 })245 */235The Response is the same as below.236237```js238new Response('Thank you for comming', {239 status: 201,240 statusText: 'Created',241 headers: {242 'X-Message': 'Hello',243 'Content-Type': 'text/plain',244 'Content-Length': '22',245 },246})247```248249### c.text()250251Render text as `Content-Type:text/plain`:251Render texts as `Content-Type:text/plain`.252253```js254app.get('/say', (c) => {255 return c.text('Hello!')
1 unmodified line
257```258259### c.json()260261Render JSON as `Content-Type:application/json`:261Render JSON as `Content-Type:application/json`.262263```js264app.get('/api', (c) => {265 return c.json({ message: 'Hello!' })
1 unmodified line
267```268269### c.html()270271Render HTML as `Content-Type:text/html`:271Render HTML as `Content-Type:text/html`.272273```js274app.get('/', (c) => {275 return c.html('<h1>Hello! Hono!</h1>')
1 unmodified line
277```278279### c.notFound()280281Return the default `404 Not Found` Response:281Return the default `404 Not Found` Response.282283```js284app.get('/notfound', (c) => {285 return c.notFound()
1 unmodified line
287```288289### c.redirect()290291Redirect, default status code is `302`:291Redirect, default status code is `302`.292293```js294app.get('/redirect', (c) => c.redirect('/'))295app.get('/redirect-permanently', (c) => c.redirect('/', 301))
32 unmodified lines
328```329330## Not Found331332If you want, you can set the default `404 Not Found` Response:332You can write the default `404 Not Found` Response.333334```js335app.notFound = (c) => {336 return c.text('This is default 404 Not Found', 404)
1 unmodified line
338```339340## Error handling341342You can handle errors in your way:342You can handle errors in your way.343344```js345app.onError = (err, c) => {346 return c.text(`This is error message: ${err.mssage}`, 500)
1 unmodified line
348```349350## fire351352`app.fire()` do:352`app.fire()` do this.353354```js355addEventListener('fetch', (event) => {356 event.respondWith(this.handleEvent(event))
11 unmodified lines
368 },369}370371/*372or just do this:372or just do:373export default app374*/375```376
4 unmodified lines
381Let's write your first code for Cloudflare Workers with Hono.382383### 1. Install Wrangler384385Install Cloudflare Command Line "[Wrangler](https://github.com/cloudflare/wrangler)"385Install Cloudflare Command Line "[Wrangler](https://github.com/cloudflare/wrangler)".386387```sh388npm i @cloudflare/wrangler -g388$ npm i @cloudflare/wrangler -g389```390391### 2. `npm init`392393Make npm skeleton directory.393Make a npm skeleton directory.394395```sh396mkdir hono-example397cd hono-example
4 unmodified lines
402403Init as a wrangler project.404405```sh406wrangler init406$ wrangler init407```408409### 4. `npm install hono`410411Install `hono` from npm registry.411Install `hono` from the npm registry.412413```sh414npm i hono414$ npm i hono415```416417### 5. Write your app418
9 unmodified lines
428```429430### 6. Run431432Run the development server locally. Then, access like `http://127.0.0.1:8787/` in your Web browser.432Run the development server locally. Then, access `http://127.0.0.1:8787/` in your Web browser.433434```sh435wrangler dev435$ wrangler dev436```437438### 7. Publish439440Deploy to Cloudflare. That's all!441442```sh443wrangler publish443$ wrangler publish444```445446## Starter template447448You can start making your application of Cloudflare Workers with [the starter template](https://github.com/yusukebe/hono-minimal). It is a realy minimal using TypeScript, esbuild, and Miniflare.449450To generate a project skelton, run this command.451452```453$ wrangler generate my-app https://github.com/yusukebe/hono-minimal454```455456## Related projects457448Implementation 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), [Sunder](https://github.com/SunderJS/sunder), and [worktop](https://github.com/lukeed/worktop) are the other routers or frameworks for Cloudflare Workers.458Implementation of the original router `TrieRouter` is inspired by [goblin](https://github.com/bmf-san/goblin). `RegExpRouter` is inspired by [Router::Boom](https://github.com/tokuhirom/Router-Boom). 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), [Sunder](https://github.com/SunderJS/sunder), and [worktop](https://github.com/lukeed/worktop) are the other routers or frameworks for Cloudflare Workers.459460- express <https://github.com/expressjs/express>461- koa <https://github.com/koajs/koa>462- itty-router <https://github.com/kwhitley/itty-router>463- Sunder <https://github.com/SunderJS/sunder>464- goblin <https://github.com/bmf-san/goblin>465- worktop <https://github.com/lukeed/worktop>466- Router::Boom <https://github.com/tokuhirom/Router-Boom>467468## Contributing469459Contributions Welcome! You can contribute by the following way:470Contributions Welcome! You can contribute by the following way.471472- Write or fix documents473- Write code of middleware474- Fix bugs475- Refactor the code476- etc.477467If you can, let's make Hono together!478Let's make Hono together!479480## Contributors481482Thanks to [all contributors](https://github.com/yusukebe/hono/graphs/contributors)!
8 unmodified lines