Skip to content
Myr

index.js

This file

Path

example/basic/index.js

102 lines · 2.4 KiB · JavaScript

Last changed by

feat: Error handling (#48)Yusuke Wadabefore adoption7c92d55

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

feat: Error handling (#48)Yusuke Wadabefore adoption7c92d55

lines 41–50why, one step back
41// Handle error
42app.use('*', async (c, next) => {43 try {44 await next()45 } catch (err) {46 console.error(`${err}`)47 c.res = new Response('Custom Error Message', { status: 500 })48 }49})50

Implemented MiddlewareYusuke Wadabefore adoptiond2058b5

line 51why, one step back
51// Routing

json method in Context (#23)Yusuke Wadabefore adoptionee80ae3

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

Feature/redirect (#36)Yusuke Wadabefore adoption4741fd3

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

Update readmeYusuke Wadabefore adoptiona41c57d

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

Feature/redirect (#36)Yusuke Wadabefore adoption4741fd3

lines 55–56why, one step back
55
56// Named parameter

Middlewares works!Yusuke Wadabefore adoptionb277869

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

Refactor (#35)Yusuke Wadabefore adoptionf9fa200

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

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

line 60why, one step back
60})

Feature/redirect (#36)Yusuke Wadabefore adoption4741fd3

lines 61–64why, one step back
61// Redirect
62app.get('/redirect', (c) => c.redirect('/'))63// Authentication required64app.get('/auth/*', (c) => c.text('You are authorized'))

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

lines 65–66why, one step back
65
66// Async

Feature/redirect (#36)Yusuke Wadabefore adoption4741fd3

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

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

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

Feature/redirect (#36)Yusuke Wadabefore adoption4741fd3

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

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

lines 70–71why, one step back
70})
71

Migrate to TypeScript (#21)Yusuke Wadabefore adoptione6039f6

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

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

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

Feature/redirect (#36)Yusuke Wadabefore adoption4741fd3

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

json method in Context (#23)Yusuke Wadabefore adoptionee80ae3

lines 76–85why, one step back
76})
7778// JSON79app.get('/api/posts', (c) => {80 const posts = [81 { id: 1, title: 'Good Morning' },82 { id: 2, title: 'Good Aternoon' },83 { id: 3, title: 'Good Evening' },84 { id: 4, title: 'Good Night' },85 ]

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

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

Middlewares works!Yusuke Wadabefore adoptionb277869

line 87why, one step back
87})

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

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

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

lines 90–92why, one step back
90
91app.post('/form', async (ctx) => {92 return ctx.json(ctx.req.parsedBody || {})

feat: Error handling (#48)Yusuke Wadabefore adoption7c92d55

lines 93–98why, one step back
93 //return new Response('ok /form')
94})9596// Throw Error97app.get('/error', () => {98 throw Error('Error has occurred')

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

line 99why, one step back
99})

Merge pull request #6 from yusukebe/developYusuke Wadabefore adoptionc0ce771

line 100why, one step back
100

Implemented MiddlewareYusuke Wadabefore adoptiond2058b5

lines 101–102why, one step back
101// addEventListener
102app.fire()