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
Provenance
- Commit
49fedca- Tree
e48a76e- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
92513d0
4 files changed+37 added−29 removed
modified:src/context.ts+1 added
14 unmodified lines151617181920212223103 unmodified linesExpand all14 unmodified lines private _status: number private _statusText: string
render: (template: string, params?: object, options?: object) => Promise<Response> notFound: () => Response
constructor( req: Request<RequestParamKeyType>, opts?: { res: Response; env: Env; event: FetchEvent }103 unmodified linesExpand allmodified:src/hono.ts+5 added-8 removed
157 unmodified lines158 const url = new URL(c.req.url)159 return url.searchParams.get(key)160 }161 162- const handler = result ? result.handler : this.notFound // XXX162+ const handler = result ? result.handler : this.notFound163 164 const middleware = []165 166 for (const mr of this.middlewareRouters) {15 unmodified lines182 middleware.push(wrappedHandler)183 184 const composed = compose<Context>(middleware)185 const c = new Context(request, { env: env, event: event, res: null })186+ c.notFound = () => this.notFound(c)187+188 await composed(c)189 190 return c.res191 }26 unmodified lines218 },219 })220 }221 220- 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 lines265 expect(res.headers.get('x-message-2')).toBe('custom-header-2')266 })267 })268 269-describe('Custom 404', () => {269+describe('404 Not Found', () => {270 const app = new Hono()271 272- 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 }275 276- 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 })279 283- app.get('/hello', () => {284- return new Response('hello')280+ app.get('/notfound', (c) => {281+ return c.notFound()282 })283 284 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 linesmodified:README.md+21 added-11 removed
119 unmodified lines120 })121 book.post('/', (c) => c.text('Create Book')) // => POST /book122 ```123 124-### Custom 404 Response125-126-You can customize 404 Not Found response:127-128-```js129-app.get('*', (c) => {130- return c.text('Custom 404 Error', 404)131-})132-```133-124 ### no strict125 126 If `strict` is set `false`, `/hello`and`/hello/` are treated the same:127 4 unmodified lines132 ```133 134 Default is `true`.135 146-## async/await136+### async/await137 138 ```js139 app.get('/fetch-url', async (c) => {140 const response = await fetch('https://example.com/')139 unmodified lines280 return c.html('<h1>Hello! Hono!</h1>')281 })282 ```283 284+### c.notFound()285+286+Return the default `404 Not Found` Response:287+288+```js289+app.get('/notfound', (c) => {290+ return c.notFound()291+})292+```293+294 ### c.redirect()295 296 Redirect, default status code is `302`:297 33 unmodified lines331 ...332 })333 ```334 335+## `404 Not Found` Response336+337+If you want, you can set the default `404 Not Found` Response:338+339+```js340+app.notFound = (c) => {341+ return c.text('This is default 404 Not Found', 404)342+}343+```344+345 ## fire346 347 `app.fire()` do:348 126 unmodified lines