Alex
Alex
HHono
Created by Alex on 10/19/2024 in #help
Can app.notFound be customized for each Hono instance?
Hi. I'm experimenting a small application on Node.js 20. I notice that the app.notFound() function is applied globally rather than per Hono instance. For example:
import { Hono } from 'hono';
import { serve } from '@hono/node-server';

const foo1 = new Hono();
foo1.get('/bar', (c) => c.text('foo1, bar'));
foo1.notFound((c) => c.text('foo1, not found'));

const foo2 = new Hono();
foo2.get('/bar', (c) => c.text('foo2, bar'));
foo2.notFound((c) => c.text('foo2, not found'));

const app = new Hono();
app.route('/foo1', foo1);
app.route('/foo2', foo2);
app.notFound((c) => c.text('app, not found'));

serve({
fetch: app.fetch,
port: 28080
});
import { Hono } from 'hono';
import { serve } from '@hono/node-server';

const foo1 = new Hono();
foo1.get('/bar', (c) => c.text('foo1, bar'));
foo1.notFound((c) => c.text('foo1, not found'));

const foo2 = new Hono();
foo2.get('/bar', (c) => c.text('foo2, bar'));
foo2.notFound((c) => c.text('foo2, not found'));

const app = new Hono();
app.route('/foo1', foo1);
app.route('/foo2', foo2);
app.notFound((c) => c.text('app, not found'));

serve({
fetch: app.fetch,
port: 28080
});
As expected, curl http://127.0.0.1:28080/foo1/bar and curl http://127.0.0.1:28080/foo2/bar get me foo1, bar and foo2, bar respectively. However, when I try curl http://127.0.0.1:28080/foo1/baz, I expect foo1, not found, but I get app, not found. Is this by design? Or I missed something? Thanks in advance.
1 replies