Middleware
Defining Middleware
Section titled “Defining Middleware”Scoped Middleware
Section titled “Scoped Middleware”Scoped middleware is defined within the routes
object. It applies to specific paths and can target all methods or specific HTTP methods.
Path Middleware
Section titled “Path Middleware”Middleware targeting a specific path is defined using "/*"
at the end of the path.
export default defineRoutes(() => ({ "/user/*": (ctx) => { Logger.log("Middleware for all /user/* routes"); },}));
Method-Specific Middleware
Section titled “Method-Specific Middleware”Middleware can also be scoped to specific HTTP methods within a path.
export default defineRoutes(() => ({ "/user/*": { POST: (ctx) => { Logger.log("Middleware for POST requests to /user/*"); }, },}));
Using the includes
Property
Section titled “Using the includes Property”Middleware can also be included directly using the includes
property. This allows adding multiple middleware functions in a concise way.
Example with Predefined Middleware
Section titled “Example with Predefined Middleware”export default defineBlock({ path: "/api", includes: [ cors({ origin: "*", }), ], routes: [...]});
Example with Custom Middleware
Section titled “Example with Custom Middleware”Create a custom middleware:
export default defineMiddleware((ctx) => { Logger.log("Custom middleware executed"); return next();});
Use the custom middleware:
export default defineBlock({ path: "/user", includes: [userMiddleware()], routes: [...]});
Example
Section titled “Example”Routes
Section titled “Routes”export default defineRoutes(() => ({ "/user/*": { POST: (ctx) => { Logger.log("POST middleware for /user/*"); }, GET: (ctx) => { Logger.log("GET middleware for /user/*"); }, }, "/user": { GET: (ctx) => { return Response.json({ message: "User Data" }); }, },}));
Block with Middleware
Section titled “Block with Middleware”export default defineBlock({ path: "/api", includes: [ cors({ origin: "*", }), userMiddleware(), ], routes: [userRoutes],});
Request Flow
Section titled “Request Flow”-
A request to
POST /api/user/123
will:- Trigger the middleware in
includes
(e.g.,cors
anduserMiddleware
). - Trigger the
POST
middleware for/user/*
. - Proceed to the route handler if no response is returned by the middleware.
- Trigger the middleware in
-
A request to
GET /api/user
will:- Trigger the middleware in
includes
(e.g.,cors
anduserMiddleware
). - Skip the
/user/*
middleware since the path does not match. - Execute the
GET
handler for/user
.
- Trigger the middleware in
Best Practices
Section titled “Best Practices”- Keep Middleware Lightweight: Avoid complex logic that might delay the request processing.
- Organize Middleware: Use meaningful path names and group related middleware into Blocks.
- Terminate or Continue Clearly: Explicitly
return next();
to make continuation clear and intentional.
Additional Notes
Section titled “Additional Notes”- Middleware runs sequentially, and order matters.
- The
includes
property provides flexibility for global middleware. - Path-based middleware now supports patterns like
"/*"
,"*"
,"*/user"
, or"/user/*"
. - Ensure that middleware does not inadvertently block further processing unless intended.
For more advanced examples, refer to the GamanJS documentation or use cases.