Skip to content

Hooks Directory Middleware

Hooks are defined with _hooks.js and run for that folder scope.

Example root hook:

import { Hook } from 'owebjs';
export default class RootHook extends Hook {
handle(req, _res, done) {
req.locals ??= {};
req.locals.trace = ['root'];
done();
}
}

Key behavior:

  • Hooks apply to the current directory and child directories
  • Nested folders can add more hooks
  • Hooks run before the route handler
  • Scoped folders using parentheses (like (admin)) creates a hook boundary

Oweb’s route walker inspects parent hook paths and looks for the nearest folder whose name is wrapped in parentheses (for example (api) or (admin)).

When such a folder exists, hook resolution is cut at that scope boundary. In practice, hooks above that scoped folder are excluded.

Example:

routes/
_hooks.js # global hook
(admin)/
_hooks.js # admin scope boundary hook
users/
_hooks.js
[id].js

For routes/(admin)/users/[id].js, Oweb uses hooks inside that scoped chain and stops climbing above the (admin) boundary.