docs: update readme (#238)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent 1a62e04 · Browse files · 1 file·+90 added−19 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+90 added−19 removed
- other:+90 added−19 removed
Provenance
- Commit
f14ec5d- Tree
37b694f- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
1a62e04
1 file changed+90 added−19 removed
modified:README.md+90 added-19 removed
50 unmodified lines51 52 Routers used in Hono are really smart.53 54 - **TrieRouter**(default) - Implemented with Trie tree structure.55-- **RegExpRouter** - Match routes with one big Regex made before dispatching at once.55+- **RegExpRouter** - Match the route with using one big Regex made before dispatch.56 57 ## Hono in 1 minute58 59 A demonstration to create an application for Cloudflare Workers with Hono.32 unmodified lines92 93 And, the routing of Hono is so flexible. It's easy to construct large web applications.94 95 ```ts96-import { Hono, Route } from 'hono'97-import { cors } from 'hono/cors'96+import { Hono } from 'hono'97+import { basicAuth } from 'hono/basic-auth'98 99-const app = new Hono()100-101-const v1 = new Route()99+const v1 = new Hono()100 v1.get('/posts', (c) => {101 return c.text('list pots')102 })105- .post('/posts', cors(), (c) => {103+ .post(basicAuth({ username, password }), (c) => {104 return c.text('created!', 201)105 })106 .get('/posts/:id', (c) => {107 const id = c.req.param('id')108 return c.text(`your id is ${id}`)109 })110 111+const app = new Hono()112 app.route('/v1', v1)113 ```114 115 ### Web Standard22 unmodified lines138 An instance of `Hono` has these methods.139 140 - app.**HTTP_METHOD**(\[path,\] handler|middleware...)141 - app.**all**(\[path,\] handler|middleware...)143-- app.**route**(path, \[Route\])142+- app.**route**(path, \[app\])143 - app.**use**(\[path,\] middleware)144 - app.**notFound**(handler)145 - app.**onError**(err, handler)146 - app.**fire**()7 unmodified lines154 ```ts155 // HTTP Methods156 app.get('/', (c) => c.text('GET /'))157 app.post('/', (c) => c.text('POST /'))158+app.put('/', (c) => c.text('PUT /'))159+app.delete('/', (c) => c.text('DELETE /'))160 161 // Wildcard162 app.get('/wild/*/card', (c) => {163 return c.text('GET /wild/*/card')11 unmodified lines175 ...176 })177 ```178 179+or all parameters at once:180+181+```ts182+app.get('/posts/:id/comment/:comment_id', (c) => {183+ const { id, comment_id } = c.req.param()184+ ...185+})186+```187+188 ### Regexp189 190 ```ts191 app.get('/post/:date{[0-9]+}/:title{[a-z]+}', (c) => {182- const date = c.req.param('date')183- const title = c.req.param('title')192+ const { date, title } = c.req.param()193 ...194 })195 ```196 30 unmodified lines227 return c.text(`Status is ${response.status}`)228 })229 ```230 222-## Route231+## Grouping232 224-`Route` object enables Nested route.233+Group the routes with `Hono` instance and add them to the main app with `route` method.234 235 ```ts227-const book = new Route()236+const book = new Hono()237 238 book.get('/', (c) => c.text('List Books')) // GET /book239 book.get('/:id', (c) => {240 // GET /book/:id1 unmodified line242 return c.text('Get Book: ' + id)243 })244 book.post('/', (c) => c.text('Create Book')) // POST /book245 246+const app = new Hono()247 app.route('/book', book)248 ```249 250 ## Middleware96 unmodified lines347 const query = c.req.query('q')348 ...349 })350 351+// Get all params at once352+app.get('/search', (c) => {353+ const { q, limit, offset } = c.req.query()354+ ...355+})356+357 // Captured params358 app.get('/entry/:id', (c) => {359 const id = c.req.param('id')360 ...81 unmodified lines442 ### c.res443 444 ```ts445 // Response object430-app.use('/', (c, next) => {431- next()446+app.use('/', async (c, next) => {447+ await next()448 c.res.headers.append('X-Debug', 'Debug message')449 })450 ```451 452 ### c.event453 454 ```ts455 // FetchEvent object440-app.use('*', async (c, next) => {456+app.get('/foo', async (c) => {457 c.event.waitUntil(442- ...458+ c.env.KV.put(key, data)459 )444- await next()460+ ...461 })462 ```463 464 ### c.env109 unmodified lines574 ```575 npx create-cloudflare my-app https://github.com/honojs/hono-minimal576 ```577 562-## Examples578+## Practical Example579 580+How about writing web API with Hono?581+582+```ts583+import { Hono } from 'hono'584+import { cors } from 'hono/cors'585+import { basicAuth } from 'hono/basic-auth'586+import { prettyJSON } from 'hono/pretty-json'587+import { getPosts, getPosts, createPost } from './model'588+589+const app = new Hono()590+app.get('/', (c) => c.text('Pretty Blog API'))591+app.use('*', prettyJSON())592+app.notFound((c) => c.json({ message: 'Not Found', ok: false }, 404))593+594+export interface Bindings {595+ USERNAME: string596+ PASSWORD: string597+}598+599+const api = new Hono<Bindings>()600+601+api.get('/posts', (c) => {602+ const { limit, offset } = c.req.query()603+ const posts = getPosts({ limit, offset })604+ return c.json({ posts })605+})606+607+api.get('/posts/:id', (c) => {608+ const id = c.req.param('id')609+ const post = getPost({ id })610+ return c.json({ post })611+})612+613+api.post(614+ '/posts',615+ async (c, next) => {616+ const auth = basicAuth({ username: c.env.USERNAME, password: c.env.PASSWORD })617+ await auth(c, next)618+ },619+ async (c) => {620+ const post = await c.req.json<POST>()621+ const ok = createPost({ post })622+ return c.json({ ok })623+ }624+)625+626+app.use('/posts/*', cors())627+628+app.route('/api', api)629+630+export default app631+```632+633+## Other Examples634+635 - Hono Examples - <https://github.com/honojs/examples>636 637 ## Related projects638 31 unmodified lines