EdgeOne Logo
Documentation
请选择
请选择
Overview
Menu

addEventListener

This API is used to register an event listener for a target. The event listener triggers an edge function when the specified type of event is delivered to the target. Only one event listener takes effect for the same type of event. Only fetch request events are supported currently. If a fetch event occurs after a fetch event listener is registered, the event listener generates a FetchEvent object to process the HTTP request.

Overview

function addEventListener(type: string, listener: (event: FetchEvent) => void): void;

Parameters

Parameter
Type
Required
Description
type
string
Yes
Event type.
Only fetch request events are supported.
If you specify a request event type other than fetch, the Edge Functions engine throws an Error exception.
listener
(event: FetchEvent) => void
Yes
Event listener that is used to process event callbacks.
You can register a fetch event listener to generate FetchEvent objects.

Sample Code

// Register a fetch event listener.
addEventListener('fetch', (event) => {
// Respond to the client.
event.respondWith(new Response('Hello World!'));
});

References