addEventListener
The addEventListener function of Azion Runtime: the fetch listener that receives each request as a FetchEvent and answers it.
The addEventListener function sets the trigger that runs your Azion Runtime code with the data of each request. The listener registers for the fetch event type and receives a FetchEvent, which carries the request. Use it to route requests by path or method, or to add authentication, rewrite headers, and redirect before a request reaches an origin. A listener can also generate personalized content or A/B test variants, or return a response it builds under your own caching logic. For more information, refer to addEventListener on MDN Web Docs.
Syntax
| Parameter | Type | Description |
|---|---|---|
type | String | Event type the listener registers for: fetch. |
listener | Function | Function the runtime calls with the FetchEvent of each incoming request. |
Example
This listener answers every request with a plain-text Response through event.respondWith():
The listener returns a response with this status, these headers, and this body: