Skip to content
Myr

index.js

This file

Path

example/basic/index.js

87 lines · 2.1 KiB · JavaScript

Last changed by

feat(body-parse): a body parse middleware (#38)Minghebefore adoption23daf7e

History of this file

Take it

Refactor (#35)Yusuke Wadabefore adoptionf9fa200

line 1why, one step back
1import { Hono, Middleware } from '../../dist'

Migrate to TypeScript (#21)Yusuke Wadabefore adoptione6039f6

line 2why, one step back
2// or install from npm:

Refactor (#35)Yusuke Wadabefore adoptionf9fa200

lines 3–4why, one step back
3// import { Hono, Middleware } from 'hono'
4

Merge pull request #10 from yusukebe/feature/dont-use-proxyYusuke Wadabefore adoptionbc34513

line 5why, one step back
5const app = new Hono()

Add exampleYusuke Wadabefore adoptione47d964

line 6why, one step back
6

Builtin Middleware!Yusuke Wadabefore adoptiond501cfd

line 7why, one step back
7// Mount Builtin Middleware

Migrate to TypeScript (#21)Yusuke Wadabefore adoptione6039f6

line 8why, one step back
8app.use('*', Middleware.poweredBy())

Logger middleware (#17)Minghebefore adoption23ba6f5

line 9why, one step back
9app.use('*', Middleware.logger())

feat(body-parse): a body parse middleware (#38)Minghebefore adoption23daf7e

line 10why, one step back
10app.use('/form', Middleware.bodyParse())

feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6

lines 11–17why, one step back
11app.use(
12 '/auth/*',13 Middleware.basicAuth({14 username: 'hono',15 password: 'acoolproject',16 })17)

Builtin Middleware!Yusuke Wadabefore adoptiond501cfd

line 18why, one step back
18

Can use async on handler (#16)Yusuke Wadabefore adoption303db32

lines 19–20why, one step back
19// Custom Middleware
20// Add Custom Header

Migrate to TypeScript (#21)Yusuke Wadabefore adoptione6039f6

line 21why, one step back
21app.use('/hello/*', async (c, next) => {

Can use async on handler (#16)Yusuke Wadabefore adoption303db32

lines 22–23why, one step back
22 await next()
23 await c.res.headers.append('X-message', 'This is addHeader middleware!')

Migrate to TypeScript (#21)Yusuke Wadabefore adoptione6039f6

line 24why, one step back
24})

Merge pull request #6 from yusukebe/developYusuke Wadabefore adoptionc0ce771

line 25why, one step back
25

Can use async on handler (#16)Yusuke Wadabefore adoption303db32

lines 26–40why, one step back
26// Log response time
27app.use('*', async (c, next) => {28 await next()29 const responseTime = await c.res.headers.get('X-Response-Time')30 console.log(`X-Response-Time: ${responseTime}`)31})3233// Add X-Response-Time header34app.use('*', async (c, next) => {35 const start = Date.now()36 await next()37 const ms = Date.now() - start38 await c.res.headers.append('X-Response-Time', `${ms}ms`)39})40

Implemented MiddlewareYusuke Wadabefore adoptiond2058b5

line 41why, one step back
41// Routing

json method in Context (#23)Yusuke Wadabefore adoptionee80ae3

line 42why, one step back
42app.get('/', (c) => c.text('Hono!!'))

Feature/redirect (#36)Yusuke Wadabefore adoption4741fd3

line 43why, one step back
43// Use Response object directly

Update readmeYusuke Wadabefore adoptiona41c57d

line 44why, one step back
44app.get('/hello', () => new Response('This is /hello'))

Feature/redirect (#36)Yusuke Wadabefore adoption4741fd3

lines 45–46why, one step back
45
46// Named parameter

Middlewares works!Yusuke Wadabefore adoptionb277869

lines 47–48why, one step back
47app.get('/entry/:id', (c) => {
48 const id = c.req.params('id')

Refactor (#35)Yusuke Wadabefore adoptionf9fa200

line 49why, one step back
49 return c.text(`Your ID is ${id}`)

Can use async on handler (#16)Yusuke Wadabefore adoption303db32

line 50why, one step back
50})

Feature/redirect (#36)Yusuke Wadabefore adoption4741fd3

lines 51–54why, one step back
51// Redirect
52app.get('/redirect', (c) => c.redirect('/'))53// Authentication required54app.get('/auth/*', (c) => c.text('You are authorized'))

Can use async on handler (#16)Yusuke Wadabefore adoption303db32

lines 55–56why, one step back
55
56// Async

Feature/redirect (#36)Yusuke Wadabefore adoption4741fd3

line 57why, one step back
57app.get('/fetch-url', async (c) => {

Can use async on handler (#16)Yusuke Wadabefore adoption303db32

line 58why, one step back
58 const response = await fetch('https://example.com/')

Feature/redirect (#36)Yusuke Wadabefore adoption4741fd3

line 59why, one step back
59 return c.text(`https://example.com/ is ${response.status}`)

Can use async on handler (#16)Yusuke Wadabefore adoption303db32

lines 60–61why, one step back
60})
61

Migrate to TypeScript (#21)Yusuke Wadabefore adoptione6039f6

line 62why, one step back
62// Request headers

Can use async on handler (#16)Yusuke Wadabefore adoption303db32

lines 63–64why, one step back
63app.get('/user-agent', (c) => {
64 const userAgent = c.req.headers.get('User-Agent')

Feature/redirect (#36)Yusuke Wadabefore adoption4741fd3

line 65why, one step back
65 return c.text(`Your UserAgent is ${userAgent}`)

json method in Context (#23)Yusuke Wadabefore adoptionee80ae3

lines 66–75why, one step back
66})
6768// JSON69app.get('/api/posts', (c) => {70 const posts = [71 { id: 1, title: 'Good Morning' },72 { id: 2, title: 'Good Aternoon' },73 { id: 3, title: 'Good Evening' },74 { id: 4, title: 'Good Night' },75 ]

status and headers args on context text/json/html method (#34)Yusuke Wadabefore adoption7920f9a

line 76why, one step back
76 return c.json(posts)

Middlewares works!Yusuke Wadabefore adoptionb277869

line 77why, one step back
77})

status and headers args on context text/json/html method (#34)Yusuke Wadabefore adoption7920f9a

lines 78–79why, one step back
78// status code
79app.post('/api/posts', (c) => c.json({ message: 'Created!' }, 201))

feat(body-parse): a body parse middleware (#38)Minghebefore adoption23daf7e

lines 80–84why, one step back
80
81app.post('/form', async (ctx) => {82 return ctx.json(ctx.req.parsedBody || {})83//return new Response('ok /form')84})

Merge pull request #6 from yusukebe/developYusuke Wadabefore adoptionc0ce771

line 85why, one step back
85

Implemented MiddlewareYusuke Wadabefore adoptiond2058b5

lines 86–87why, one step back
86// addEventListener
87app.fire()