Middlewares
Middlewares are functions that execute sequentially before the handler or validator if presented.
(ctx) => { // Do something with the context}
If a middleware returns a Response
object, the object is returned as a response directly.
To skip checking this you can use app.prepare
with the same arguments instead of app.use
.
If a middleware returns a Promise
it should be an async function for the compiler to detect and inline the validator call.
To register middlewares for all handlers of a Byte
instance:
// Register one middlewareapp.use((ctx) => { if (ctx.req.headers.get('Origin') !== 'https://example.com') { ctx.status = 403; return ctx.body('Rejected'); }});
// Handler will run after middlewareapp.get('/middlewares', (ctx) => ctx.body('Hi'));