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 fileTake it
Refactor (#35)Yusuke Wadabefore adoptionf9fa200
line 1why, one step back1import { Hono, Middleware } from '../../dist'Migrate to TypeScript (#21)Yusuke Wadabefore adoptione6039f6
line 2why, one step back2// or install from npm:Refactor (#35)Yusuke Wadabefore adoptionf9fa200
lines 3–4why, one step back3// import { Hono, Middleware } from 'hono'4Merge pull request #10 from yusukebe/feature/dont-use-proxyYusuke Wadabefore adoptionbc34513
line 5why, one step back5const app = new Hono()Add exampleYusuke Wadabefore adoptione47d964
line 6why, one step back6Builtin Middleware!Yusuke Wadabefore adoptiond501cfd
line 7why, one step back7// Mount Builtin MiddlewareMigrate to TypeScript (#21)Yusuke Wadabefore adoptione6039f6
line 8why, one step back8app.use('*', Middleware.poweredBy())Logger middleware (#17)Minghebefore adoption23ba6f5
line 9why, one step back9app.use('*', Middleware.logger())feat(body-parse): a body parse middleware (#38)Minghebefore adoption23daf7e
line 10why, one step back10app.use('/form', Middleware.bodyParse())feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
lines 11–17why, one step back11app.use(12 '/auth/*',13 Middleware.basicAuth({14 username: 'hono',15 password: 'acoolproject',16 })17)Builtin Middleware!Yusuke Wadabefore adoptiond501cfd
line 18why, one step back18Can use async on handler (#16)Yusuke Wadabefore adoption303db32
lines 19–20why, one step back19// Custom Middleware20// Add Custom HeaderMigrate to TypeScript (#21)Yusuke Wadabefore adoptione6039f6
line 21why, one step back21app.use('/hello/*', async (c, next) => {Can use async on handler (#16)Yusuke Wadabefore adoption303db32
lines 22–23why, one step back22 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 back24})Merge pull request #6 from yusukebe/developYusuke Wadabefore adoptionc0ce771
line 25why, one step back25Can use async on handler (#16)Yusuke Wadabefore adoption303db32
lines 26–40why, one step back26// Log response time27app.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})40feat: Error handling (#48)Yusuke Wadabefore adoption7c92d55
lines 41–50why, one step back41// Handle error42app.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})50Implemented MiddlewareYusuke Wadabefore adoptiond2058b5
line 51why, one step back51// Routingjson method in Context (#23)Yusuke Wadabefore adoptionee80ae3
line 52why, one step back52app.get('/', (c) => c.text('Hono!!'))Feature/redirect (#36)Yusuke Wadabefore adoption4741fd3
line 53why, one step back53// Use Response object directlyUpdate readmeYusuke Wadabefore adoptiona41c57d
line 54why, one step back54app.get('/hello', () => new Response('This is /hello'))Feature/redirect (#36)Yusuke Wadabefore adoption4741fd3
lines 55–56why, one step back5556// Named parameterMiddlewares works!Yusuke Wadabefore adoptionb277869
lines 57–58why, one step back57app.get('/entry/:id', (c) => {58 const id = c.req.params('id')Refactor (#35)Yusuke Wadabefore adoptionf9fa200
line 59why, one step back59 return c.text(`Your ID is ${id}`)Can use async on handler (#16)Yusuke Wadabefore adoption303db32
line 60why, one step back60})Feature/redirect (#36)Yusuke Wadabefore adoption4741fd3
lines 61–64why, one step back61// Redirect62app.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 back6566// AsyncFeature/redirect (#36)Yusuke Wadabefore adoption4741fd3
line 67why, one step back67app.get('/fetch-url', async (c) => {Can use async on handler (#16)Yusuke Wadabefore adoption303db32
line 68why, one step back68 const response = await fetch('https://example.com/')Feature/redirect (#36)Yusuke Wadabefore adoption4741fd3
line 69why, one step back69 return c.text(`https://example.com/ is ${response.status}`)Can use async on handler (#16)Yusuke Wadabefore adoption303db32
lines 70–71why, one step back70})71Migrate to TypeScript (#21)Yusuke Wadabefore adoptione6039f6
line 72why, one step back72// Request headersCan use async on handler (#16)Yusuke Wadabefore adoption303db32
lines 73–74why, one step back73app.get('/user-agent', (c) => {74 const userAgent = c.req.headers.get('User-Agent')Feature/redirect (#36)Yusuke Wadabefore adoption4741fd3
line 75why, one step back75 return c.text(`Your UserAgent is ${userAgent}`)json method in Context (#23)Yusuke Wadabefore adoptionee80ae3
lines 76–85why, one step back76})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 back86 return c.json(posts)Middlewares works!Yusuke Wadabefore adoptionb277869
line 87why, one step back87})status and headers args on context text/json/html method (#34)Yusuke Wadabefore adoption7920f9a
lines 88–89why, one step back88// status code89app.post('/api/posts', (c) => c.json({ message: 'Created!' }, 201))feat(body-parse): a body parse middleware (#38)Minghebefore adoption23daf7e
lines 90–92why, one step back9091app.post('/form', async (ctx) => {92 return ctx.json(ctx.req.parsedBody || {})feat: Error handling (#48)Yusuke Wadabefore adoption7c92d55
lines 93–98why, one step back93 //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 back99})Merge pull request #6 from yusukebe/developYusuke Wadabefore adoptionc0ce771
line 100why, one step back100Implemented MiddlewareYusuke Wadabefore adoptiond2058b5
lines 101–102why, one step back101// addEventListener102app.fire()