Skip to content
Workers
Visit Workers on GitHub
Set theme to dark (⇧+D)

addEventListener

Background

This function defines triggers for a Worker script to execute.

The only type and event type supported are "fetch" and FetchEvent, respectively.

Syntax

  • addEventListener(type, listener) void
    • If multiple listeners are registered, when one doesn’t call event.respondWith(), the runtime delivers the event to the next registered listener.

Properties

  • type string

    • Currently the only type supported is "fetch".
  • listener function

    • The function to handle incoming requests to the Worker script. The listener is passed a single argument:

Example

addEventListener("fetch", event => {  return event.respondWith(    new Response("Hello world")  )})