Skip to content
Myr

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

1 file+90 added−19 removed

modified:README.md+90 added-19 removed
50 unmodified lines
5152Routers used in Hono are really smart.5354- **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.5657## Hono in 1 minute5859A demonstration to create an application for Cloudflare Workers with Hono.
32 unmodified lines
9293And, the routing of Hono is so flexible. It's easy to construct large web applications.9495```ts96import { Hono, Route } from 'hono'97import { cors } from 'hono/cors'96import { Hono } from 'hono'97import { basicAuth } from 'hono/basic-auth'9899const app = new Hono()100101const v1 = new Route()99const v1 = new Hono()100v1.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 })110111const app = new Hono()112app.route('/v1', v1)113```114115### Web Standard
22 unmodified lines
138An instance of `Hono` has these methods.139140- 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 lines
154```ts155// HTTP Methods156app.get('/', (c) => c.text('GET /'))157app.post('/', (c) => c.text('POST /'))158app.put('/', (c) => c.text('PUT /'))159app.delete('/', (c) => c.text('DELETE /'))160161// Wildcard162app.get('/wild/*/card', (c) => {163 return c.text('GET /wild/*/card')
11 unmodified lines
175 ...176})177```178179or all parameters at once:180181```ts182app.get('/posts/:id/comment/:comment_id', (c) => {183 const { id, comment_id } = c.req.param()184 ...185})186```187188### Regexp189190```ts191app.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 lines
227 return c.text(`Status is ${response.status}`)228})229```230222## Route231## Grouping232224`Route` object enables Nested route.233Group the routes with `Hono` instance and add them to the main app with `route` method.234235```ts227const book = new Route()236const book = new Hono()237238book.get('/', (c) => c.text('List Books')) // GET /book239book.get('/:id', (c) => {240 // GET /book/:id
1 unmodified line
242 return c.text('Get Book: ' + id)243})244book.post('/', (c) => c.text('Create Book')) // POST /book245246const app = new Hono()247app.route('/book', book)248```249250## Middleware
96 unmodified lines
347 const query = c.req.query('q')348 ...349})350351// Get all params at once352app.get('/search', (c) => {353 const { q, limit, offset } = c.req.query()354 ...355})356357// Captured params358app.get('/entry/:id', (c) => {359 const id = c.req.param('id')360 ...
81 unmodified lines
442### c.res443444```ts445// Response object430app.use('/', (c, next) => {431 next()446app.use('/', async (c, next) => {447 await next()448 c.res.headers.append('X-Debug', 'Debug message')449})450```451452### c.event453454```ts455// FetchEvent object440app.use('*', async (c, next) => {456app.get('/foo', async (c) => {457 c.event.waitUntil(442 ...458 c.env.KV.put(key, data)459 )444 await next()460 ...461})462```463464### c.env
109 unmodified lines
574```575npx create-cloudflare my-app https://github.com/honojs/hono-minimal576```577562## Examples578## Practical Example579580How about writing web API with Hono?581582```ts583import { Hono } from 'hono'584import { cors } from 'hono/cors'585import { basicAuth } from 'hono/basic-auth'586import { prettyJSON } from 'hono/pretty-json'587import { getPosts, getPosts, createPost } from './model'588589const app = new Hono()590app.get('/', (c) => c.text('Pretty Blog API'))591app.use('*', prettyJSON())592app.notFound((c) => c.json({ message: 'Not Found', ok: false }, 404))593594export interface Bindings {595 USERNAME: string596 PASSWORD: string597}598599const api = new Hono<Bindings>()600601api.get('/posts', (c) => {602 const { limit, offset } = c.req.query()603 const posts = getPosts({ limit, offset })604 return c.json({ posts })605})606607api.get('/posts/:id', (c) => {608 const id = c.req.param('id')609 const post = getPost({ id })610 return c.json({ post })611})612613api.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)625626app.use('/posts/*', cors())627628app.route('/api', api)629630export default app631```632633## Other Examples634635- Hono Examples - <https://github.com/honojs/examples>636637## Related projects638
31 unmodified lines