# Exemplos JavaScript - Functions no Firewall

Exemplo de uso geral para functions executadas no firewall, combinando várias ações de tratamento de requisição em um único handler. Use-o como ponto de partida quando precisar inspecionar headers e metadados de entrada e então decidir se vai bloquear, descartar, modificar ou responder a uma requisição diretamente na rede global da Azion. O exemplo a seguir demonstra as seguintes capacidades:

- `event.deny()`
- `event.drop()`
- `event.addRequestHeader()`
- `event.addResponseHeader()`
- `event.respondWith()`
- `event.continue()`

```js
    async function firewallHandler(event) {
        // Access the value of x-deny, one of the headers
        let condition = event.request.headers.get("x-deny");
    
        if (condition) {
            // event.deny() triggers a default 403 Response
            event.deny();
        }
    
        let anotherCondition = event.request.headers.get("x-drop"); // Access the value of x-drop, , one of the headers
        if (anotherCondition) {
            // event.drop() drops the connection without returning a Response
            event.drop();
        }
    
        // Access the value of x-third, , one of the headers
        let aThirdCondition = event.request.headers.get("x-third");
        if (aThirdCondition) {
            // event.addRequestHeader() adds a new header to the Request object, passing key:"value".
            event.addRequestHeader("X-New-Request-Header", "Hello");
        }
    
        // Access the value of x-fourth, one of the headers
        let aFourthCondition = event.request.headers.get("x-fourth");
        if (aFourthCondition) {
            //  event.addResponseHeader() adds a new header to the Response object
            //  that will be delivered after accessing the origin. 
            event.addResponseHeader("X-New-Response-Header", "Bye");
        }
    
        // The metadata object contains the attributes of the request
        if (event.request.metadata["remote_addr"] == "127.0.0.1") {
            // event.respondWith(new Response()) allows you to serve your own Response
            event.respondWith(new Response('{"my_custom_response": true}', {
                status: 599,
                headers: { "content-type": "application/json" }
            }));
        }
    
        // If the request was not denied or dropped and did not have a Response returned,
        // it is necessary to continue the processing, and to do so use use -> event.continue()
        // Do not forget this command, otherwise the request won't continue!
        event.continue();
    }

    addEventListener("firewall", (event) => event.waitUntil(firewallHandler(event)));
```

## Como funciona

O handler é executado no evento `firewall` dentro de `event.waitUntil()` e lê vários headers da requisição com `event.request.headers.get()` para orientar suas decisões. Dependendo de qual header está presente, ele pode chamar `event.deny()` para retornar um 403, `event.drop()` para encerrar a conexão sem resposta, ou `event.addRequestHeader()` e `event.addResponseHeader()` para injetar headers personalizados. Ele também pode interromper o fluxo com `event.respondWith(new Response(...))` para servir um corpo JSON próprio e termina com `event.continue()`, de modo que qualquer requisição ainda não tratada prossiga normalmente.

## Recursos relacionados

- [Exemplos em JavaScript](/pt-br/documentacao/devtools/javascript-exemplos/)
- [Runtime APIs](/pt-br/documentacao/produtos/applications/functions/runtime-apis/javascript/fetch-event/)


---