Skip to content
Myr

feat: `app.notFound` for default 404 not found (#110)

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 92513d0 · Browse files · 4 files+37 added−29 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+37 added−29 removed

  • tests:+10 added−10 removed
  • other:+27 added−19 removed

4 files+37 added−29 removed

modified:src/context.ts+1 added
modified:src/hono.ts+5 added-8 removed
157 unmodified lines
158 const url = new URL(c.req.url)159 return url.searchParams.get(key)160 }161162 const handler = result ? result.handler : this.notFound // XXX162 const handler = result ? result.handler : this.notFound163164 const middleware = []165166 for (const mr of this.middlewareRouters) {
15 unmodified lines
182 middleware.push(wrappedHandler)183184 const composed = compose<Context>(middleware)185 const c = new Context(request, { env: env, event: event, res: null })186 c.notFound = () => this.notFound(c)187188 await composed(c)189190 return c.res191 }
26 unmodified lines
218 },219 })220 }221220 notFound() {222 notFound(c: Context) {223 const message = 'Not Found'222 return new Response(message, {223 status: 404,224 headers: {225 'Content-Length': message.length.toString(),226 },227 })224 return c.text(message, 404)225 }226}
modified:test/hono.test.ts+10 added-10 removed
264 unmodified lines
265 expect(res.headers.get('x-message-2')).toBe('custom-header-2')266 })267})268269describe('Custom 404', () => {269describe('404 Not Found', () => {270 const app = new Hono()271272 app.notFound = () => {273 return new Response('Default 404 Nout Found', { status: 404 })272 app.notFound = (c) => {273 return c.text('Custom 404 Not Found', 404)274 }275276 app.use('*', async (c, next) => {277 await next()278 if (c.res.status === 404) {279 c.res = new Response('Custom 404 Not Found', { status: 404 })280 }276 app.get('/hello', (c) => {277 return c.text('hello')278 })279283 app.get('/hello', () => {284 return new Response('hello')280 app.get('/notfound', (c) => {281 return c.notFound()282 })283284 it('Custom 404 Not Found', async () => {285 let req = new Request('http://localhost/hello')286 let res = await app.dispatch(req)287 expect(res.status).toBe(200)288 req = new Request('http://localhost/notfound')289 res = await app.dispatch(req)290 expect(res.status).toBe(404)291 req = new Request('http://localhost/foo')292 res = await app.dispatch(req)293 expect(res.status).toBe(404)294 expect(await res.text()).toBe('Custom 404 Not Found')
90 unmodified lines
modified:README.md+21 added-11 removed
119 unmodified lines
120})121book.post('/', (c) => c.text('Create Book')) // => POST /book122```123124### Custom 404 Response125126You can customize 404 Not Found response:127128```js129app.get('*', (c) => {130 return c.text('Custom 404 Error', 404)131})132```133124### no strict125126If `strict` is set `false`, `/hello`and`/hello/` are treated the same:127
4 unmodified lines
132```133134Default is `true`.135146## async/await136### async/await137138```js139app.get('/fetch-url', async (c) => {140 const response = await fetch('https://example.com/')
139 unmodified lines
280 return c.html('<h1>Hello! Hono!</h1>')281})282```283284### c.notFound()285286Return the default `404 Not Found` Response:287288```js289app.get('/notfound', (c) => {290 return c.notFound()291})292```293294### c.redirect()295296Redirect, default status code is `302`:297
33 unmodified lines
331 ...332})333```334335## `404 Not Found` Response336337If you want, you can set the default `404 Not Found` Response:338339```js340app.notFound = (c) => {341 return c.text('This is default 404 Not Found', 404)342}343```344345## fire346347`app.fire()` do:348
126 unmodified lines