Place a use.ts file in any folder, and its middleware automatically wraps all routes in that folder and its subfolders - no imports or wiring required.
π― How it Works β
api/users/
βββ about/
β βββ index.ts
βββ account/
β βββ index.ts
β βββ use.ts
βββ index.ts
βββ use.tsusers/use.tswraps all routes under/api/usersusers/account/use.tswraps only routes under/api/users/account
Execution order for a request to /api/users/account:
api/use.ts β global middleware
users/use.ts β parent folder
users/account/use.ts β current folder
users/account/index.ts β route handlerParent middleware always runs before child middleware. Child routes cannot skip parent use.ts.
The generated boilerplate when you create a new use.ts:
import { use } from "_/api";
export default [
use(async (ctx, next) => {
return next();
})
];Some editors load the generated content immediately, others require a brief unfocus/refocus.
πΌ Common Use Cases β
Authentication β
export default [
use(async (ctx, next) => {
const token = ctx.headers.authorization?.replace("Bearer ", "");
ctx.assert(token, 401, "Authentication required");
const user = await verifyToken(token);
ctx.assert(user.role === "admin", 403, "Admin access required");
ctx.state.user = user;
return next();
})
];import { HTTPException } from "hono/http-exception";
export default [
use(async (ctx, next) => {
const token = ctx.req.header("authorization")?.replace("Bearer ", "");
if (!token) throw new HTTPException(401, { message: "Authentication required" });
const user = await verifyToken(token);
if (user.role !== "admin") throw new HTTPException(403, { message: "Admin access required" });
ctx.set("user", user);
return next();
})
];Every route under /api/admin now requires admin auth. Route handlers can assume the user is already validated (ctx.state.user for Koa, ctx.get("user") for Hono).
Request Logging β
export default [
use(async (ctx, next) => {
const start = Date.now();
const requestId = crypto.randomUUID();
ctx.state.requestId = requestId;
console.log(`[${requestId}] ${ctx.method} ${ctx.path}`);
try {
await next();
} finally {
console.log(`[${requestId}] completed in ${Date.now() - start}ms`);
}
})
];export default [
use(async (ctx, next) => {
const start = Date.now();
const requestId = crypto.randomUUID();
ctx.set("requestId", requestId);
console.log(`[${requestId}] ${ctx.req.method} ${ctx.req.path}`);
await next();
console.log(`[${requestId}] completed in ${Date.now() - start}ms`);
})
];Rate Limiting β
import rateLimit from "koa-ratelimit";
export default [
use(
rateLimit({
driver: "memory",
db: new Map(),
duration: 60000,
max: 100,
})
)
];import { rateLimiter } from "hono-rate-limiter";
export default [
use(
rateLimiter({
windowMs: 60000,
limit: 100,
keyGenerator: (ctx) => ctx.req.header("x-forwarded-for") ?? "",
}),
)
];β οΈ Parameter Availability β
Cascading middleware runs for all routes in the hierarchy, including ones that don't define the parameters you might expect:
api/users/
βββ [id]/index.ts β has 'id' param
βββ index.ts β NO 'id' param
βββ use.tsctx.params.id is undefined for /users. Keep cascading middleware generic - authentication, logging, rate limiting. Parameter-specific logic belongs in the route handler.
π¨ Multiple Middleware + Method Filtering β
A single use.ts can define multiple functions, and each supports the on option:
export default [
use(async (ctx, next) => {
console.log(`${ctx.method} ${ctx.path}`);
return next();
}),
use(
async (ctx, next) => {
const token = ctx.headers.authorization?.replace("Bearer ", "");
ctx.assert(token, 401, "Authentication required");
ctx.state.user = await verifyToken(token);
return next();
},
{ on: ["POST", "PUT", "PATCH", "DELETE"] },
),
use(async (ctx, next) => {
const start = Date.now();
await next();
ctx.set("X-Response-Time", `${Date.now() - start}ms`);
}),
];import { HTTPException } from "hono/http-exception";
export default [
use(async (ctx, next) => {
console.log(`${ctx.req.method} ${ctx.req.path}`);
return next();
}),
use(
async (ctx, next) => {
const token = ctx.req.header("authorization")?.replace("Bearer ", "");
if (!token) throw new HTTPException(401, { message: "Authentication required" });
ctx.set("user", await verifyToken(token));
return next();
},
{ on: ["POST", "PUT", "PATCH", "DELETE"] },
),
use(async (ctx, next) => {
const start = Date.now();
await next();
ctx.header("X-Response-Time", `${Date.now() - start}ms`);
}),
];