v0.9.0
In this release:
- Add support for app.route(method, path, handler)
- bun: export Request (for consistency; even though it's already a global)
- bun: update tests
- bun: update dependency
bun-types
- bun: Always use ESM exports (fixes examples/bun-app)
- express: update script in package.json (fixes yarn clean)
Notes
As of this release, we now support a middleware-like wildcard route handler, which also enables custom 404 fallback. See example below.
import { attachRoutes, defineRoutes } from './application';
import * as handlers from './handlers';
const port = 3000;
const middleware = defineRoutes((app) => [
app.route('*', '/*', (request) => {
const authHeader = request.headers.get('authorization');
if (authHeader !== '123') {
throw new Error('Invalid auth');
}
}),
]);
const fallback = defineRoutes((app) => [
app.route('*', '/*', (request) => {
const message = `No resource found at path "${request.path}"`;
return new Response(message, { status: 404 });
}),
]);
Bun.serve({
port,
fetch: attachRoutes(middleware, ...Object.values(handlers), fallback),
});