Handle 404
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by Yusuke Wada · parent 8c96d99 · Browse files · 3 files·+48 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+48 added−7 removed
- tests:+29 added−0 removed
- other:+19 added−7 removed
Provenance
- Commit
1b77b0f- Tree
feddea1- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
Yusuke Wada <yusuke@kamawada.com>- Parent
8c96d99
3 files changed+48 added−7 removed
modified:src/hono.js+2 added-3 removed
81 unmodified lines828384858686878890899091929326 unmodified lines1201211221231251241251261271287 unmodified lines81 unmodified lines async dispatch(request, response) { const [method, path] = [request.method, getPathFromURL(request.url)]
const result = await this.matchRoute(method, path) if (!result) return this.notFound()
request.params = (key) => result.params[key]
let handler = result.handler[0] // XXX let handler = result ? result.handler[0] : this.notFound // XXX
const middleware = [Middleware.defaultFilter] // add defaultFilter later
for (const mr of this.middlewareRouters) {26 unmodified lines event.respondWith(this.handleEvent(event)) }) }
notFound() { async notFound() { return new Response('Not Found', { status: 404 }) }}
7 unmodified linesmodified:src/hono.test.js+29 added
111 unmodified lines112 expect(await res.headers.get('x-custom')).toBe('root')113 expect(await res.headers.get('x-message-2')).toBe('custom-header-2')114 })115 })116+117+describe('Custom 404', () => {118+ const app = new Hono()119+120+ const customNotFound = async (c, next) => {121+ next()122+ if (c.res.status === 404) {123+ c.res = new fetch.Response('Custom 404 Not Found', { status: 404 })124+ }125+ }126+127+ app.notFound = () => {128+ return new fetch.Response('Default 404 Nout Found', { status: 404 })129+ }130+131+ app.use('*', customNotFound)132+ app.get('/hello', () => {133+ return new fetch.Response('hello')134+ })135+ it('Custom 404 Not Found', async () => {136+ let req = new fetch.Request('https://example.com/hello')137+ let res = await app.dispatch(req)138+ expect(res.status).toBe(200)139+ req = new fetch.Request('https://example.com/foo')140+ res = await app.dispatch(req)141+ expect(res.status).toBe(404)142+ expect(await res.text()).toBe('Custom 404 Not Found')143+ })144+})modified:README.md+17 added-4 removed
114 unmodified lines115 116 ### Custom Middleware117 118 ```js119-const logger = (c, next) => {119+const logger = async (c, next) => {120 console.log(`[${c.req.method}] ${c.req.url}`)121 next()122 }123 124-const addHeader = (c, next) => {124+const addHeader = async (c, next) => {125 next()126 c.res.headers.add('x-message', 'This is middleware!')127 }128 2 unmodified lines131 132 app.get('/message/hello', () => 'Hello Middleware!')133 ```134 135+### Custom 404 Response136+137+```js138+const customNotFound = async (c, next) => {139+ next()140+ if (c.res.status === 404) {141+ c.res = new Response('Custom 404 Not Found', { status: 404 })142+ }143+}144+145+app.use('*', customNotFound)146+```147+148 ## Context149 150 ### req151 6 unmodified lines158 159 ### res160 161 ```js149-app.use('/', (c, next) => {162+app.use('/', async (c, next) => {163 next()151- c.res.headers.append('X-Debug', 'Debug message')164+ await c.res.headers.append('X-Debug', 'Debug message')165 })166 ```167 168 ## Request35 unmodified lines