index.js
This file
Path
examples/basic/index.js
115 lines · 2.8 KiB · JavaScript
Last changed by
chore: do not need `await`Yusuke Wadabefore adoptiona8e0bf0
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116import { Hono } from '../../dist'import { poweredBy } from '../../src/middleware/powered-by/powered-by'import { logger } from '../../src/middleware/logger/logger'import { bodyParse } from '../../src/middleware/body-parse/body-parse'import { basicAuth } from '../../src/middleware/basic-auth/basic-auth'import { etag } from '../../src/middleware/etag/etag'
const app = new Hono()
// Mount Builtin Middlewareapp.use('*', poweredBy())app.use('*', logger())app.use('/form', bodyParse())app.use( '/auth/*', basicAuth({ username: 'hono', password: 'acoolproject', }))app.use('/etag/*', etag())
// Custom Middleware// Add Custom Headerapp.use('/hello/*', async (c, next) => { await next() c.header('X-message', 'This is addHeader middleware!')})
// Add X-Response-Time headerapp.use('*', async (c, next) => { const start = Date.now() await next() const ms = Date.now() - start c.header('X-Response-Time', `${ms}ms`)})
// Custom Not Found Messageapp.notFound((c) => { return c.text('Custom 404 Not Found', 404)})
// Error handlingapp.onError((err, c) => { console.error(`${err}`) return c.text('Custom Error Message', 500)})
// Routingapp.get('/', (c) => c.text('Hono!!'))// Use Response object directlyapp.get('/hello', () => new Response('This is /hello'))
// Named parameterapp.get('/entry/:id', (c) => { const id = c.req.param('id') return c.text(`Your ID is ${id}`)})
// Nested routeconst book = app.route('/book')book.get('/', (c) => c.text('List Books'))book.get('/:id', (c) => { const id = c.req.param('id') return c.text('Get Book: ' + id)})book.post('/', (c) => c.text('Create Book'))
// Redirectapp.get('/redirect', (c) => c.redirect('/'))// Authentication requiredapp.get('/auth/*', (c) => c.text('You are authorized'))// ETagapp.get('/etag/cached', (c) => c.text('Is this cached?'))
// Asyncapp.get('/fetch-url', async (c) => { const response = await fetch('https://example.com/') return c.text(`https://example.com/ is ${response.status}`)})
// Request headersapp.get('/user-agent', (c) => { const userAgent = c.req.header('User-Agent') return c.text(`Your UserAgent is ${userAgent}`)})
// JSONapp.get('/api/posts', (c) => { const posts = [ { id: 1, title: 'Good Morning' }, { id: 2, title: 'Good Aternoon' }, { id: 3, title: 'Good Evening' }, { id: 4, title: 'Good Night' }, ] return c.json(posts)})// status codeapp.post('/api/posts', (c) => c.json({ message: 'Created!' }, 201))// default routeapp.get('/api/*', (c) => c.text('API endpoint is not found', 404))
app.post('/form', async (c) => { return c.json(c.req.parsedBody || {}) //return new Response('ok /form')})
// Throw Errorapp.get('/error', () => { throw Error('Error has occurred')})app.get('/type-error', () => 'return not Response instance')
// addEventListenerapp.fire()