Runtime
At build time, Marko Run compiles route files into a router that creates a context object for each request and passes it through middleware, handlers, and templates. Adapters wire this router into deployment targets automatically, or it can be embedded directly in an existing server.
Context
Passed as the first argument (ctx) to middleware and handlers, and available as $global in templates:
| Property | Description |
|---|---|
route | Matched route path pattern string |
request | WHATWG Request for the incoming request |
method | HTTP method string |
url | WHATWG URL for the request |
signal | AbortSignal that aborts on client disconnect ($global.signal) |
params | Path parameters, transformed by any params validator |
search | Query string values, transformed by any search validator |
body | Lazy promise for the validated body when a json or form validator is configured, else undefined |
data | Values passed downstream via next(data) |
meta | Static metadata from +meta |
platform | Adapter-specific platform data |
parent | Calling context for internal ctx.fetch requests, else undefined |
$global.params and $global.url are serialized to the browser by default so client code can read them after resume. Add other properties through ctx.serializedGlobals (see $global.serializedGlobals).
Context Methods
fetch
fetch(resource: string | URL | Request, init?: RequestInit): Promise<Response>;Dispatches an internal request through the router using the standard fetch signature. Relative URLs resolve against ctx.url, and the child request's ctx.parent points to the caller's context.
render
render<T>(template: Marko.Template<T>, input: T, init?: ResponseInit): Response;Streams a Marko template as a Response (default status 200, Content-Type: text/html;charset=UTF-8 unless set in init.headers, empty body for HEAD).
redirect
redirect(to: string | URL, status?: number): Response;Returns a redirect response resolving relative paths against ctx.url. Defaults to 302; accepts 301, 302, 303, 307, or 308.
back
back(fallback?: string | URL, status?: number): Response;Redirects to the request's Referer header when it shares the same origin as ctx.url, otherwise falls back to fallback (default "/").
Typed URLs
Run.href builds URI-encoded URLs checked against the application's routes at compile time when generated types are enabled:
<a href=Run.href("/projects/$projectId/members", {
params: { projectId: 42 },
search: { sort: "name" },
hash: "top",
})>
Members
</a>a href=Run.href("/projects/$projectId/members", {
params: { projectId: 42 },
search: { sort: "name" },
hash: "top",
}) --
Members| Option | Description |
|---|---|
params | Values for dynamic segments; required when present; $$ also accepts arrays joined with / |
search | Query string values, typed against the route's search validator when declared |
hash | Fragment appended after # |
In client builds, Marko Run compiles direct Run.href(...) calls away when statically analyzable. Call Run.href directly in browser code rather than aliasing or destructuring Run.
Embedding
Import @marko/run/router directly to embed routing inside a custom server:
import * as router from "@marko/run/router";router.fetch
function fetch(request: Request, platform: unknown): Promise<Response | void>;Executes the matched route for a WHATWG Request with platform exposed as ctx.platform. Returns:
- a WHATWG
Responsefrom the matched route undefinedif the route exited unhandled (throw null)- a
404response if no route matched - a
500response if an uncaught error occurred
import express from "express";
import * as router from "@marko/run/router";
express()
.use(async (req, res, next) => {
const request = createWHATWGRequest(req);
const response = await router.fetch(request, { req, res });
if (response) {
applyResponse(response, res);
} else {
next();
}
})
.listen(3000);For Express and Connect-style Node.js servers, the Node adapter's middleware handles request/response conversion automatically.
router.match
function match(method: string, pathname: string): RouteMatch | null;Synchronously matches an HTTP method and pathname, returning a RouteMatch (params, meta, path, handler, options) or null when unmatched.
router.invoke
function invoke(
route: RouteMatch | null,
request: Request,
platform: unknown,
url?: URL,
): Promise<Response | void>;Executes a RouteMatch returned by router.match (passing null triggers 404 handling), letting custom middleware inspect route metadata between matching and invocation:
import express from "express";
import * as router from "@marko/run/router";
express()
.use((req, res, next) => {
res.locals.match = router.match(req.method, req.path);
next();
})
// middleware that inspects `res.locals.match`
.use(async (req, res, next) => {
if (!res.locals.match) {
next();
return;
}
const request = createWHATWGRequest(req);
const response = await router.invoke(res.locals.match, request, { req, res });
if (response) {
applyResponse(response, res);
} else {
next();
}
})
.listen(3000);Next Steps
Contributors
Helpful? You can thank these awesome people! You can also edit this doc if you see any issues or want to improve it.