# addEventListener

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](/en/documentation/devtools/runtime/api-reference/fetch-event/), 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](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener) on MDN Web Docs.

> **Note**
>
> Under `azion dev`, hot reload cannot switch an `export default` entry to `addEventListener`: the server stops with `No matching export in "index.js" for import "default"`. Start `azion dev` again to serve the new entry.

---

## Syntax

```javascript
addEventListener(type, listener)
```

| 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](/en/documentation/devtools/runtime/api-reference/response/) through `event.respondWith()`:

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

The listener returns a response with this status, these headers, and this body:

```json
{
 "status": 200,
 "statusText": "",
 "headers": {
  "content-type": "text/plain;charset=UTF-8"
 },
 "body": "Hello world"
}
```

---

## Related resources

- [FetchEvent](/en/documentation/devtools/runtime/api-reference/fetch-event.md): The event a `fetch` listener receives, and the request it carries.
- [Handlers](/en/documentation/devtools/runtime/api-reference/handlers.md): The recommended `export default { fetch }` entry for a function.
- [fetch](/en/documentation/devtools/runtime/api-reference/fetch.md): How a function sends its own request and reads the response.
- [Web APIs](/en/documentation/devtools/runtime/api-reference/javascript.md): The other Web APIs that Azion Runtime supports.
