Functions first steps
Before you begin
For you to get started and excel as an edge developer, it’s important to:
- Be familiar with JavaScript.
- Know the concept of Strict mode.
- Have an account on Azion’s platform.
Azion Functions works on the Azion Runtime, which is a set of tools that enable the development.
It’s paramount to take a look at the list of available APIs, methods, and types. Learn more about Azion Runtime.
Code Editor
The Functions Code Editor provides a development experience similar to what developers are accustomed to. The Functions ChatGPT Integration helps you to write, refactor, and review code.
Functions and Applications
Developing your first function for Applications:
- Access Azion Console > Functions.
- Click + Function.
- Choose a name for your function.
- Write your function. But wait, before that, keep reading:
Writing a function
First, the functions for applications work based on a fetch event. It’s initialized with an addEventListener function, passing fetch as the event type, and an event. For example:
addEventListener('fetch', event => { event.respondWith(handleRequest(event.request)); });Second, it’s necessary to define the behavior of the handleRequest function. This function has the event.request as the signature. This data can be used later on to implement the necessary logic, such as:
- Manipulate cookies.
- Implement a behavior based on the request method (POST, GET, PUT, DELETE).
- Access request metadata.
The handleRequest function can be defined as:
const html = `<!DOCTYPE html> <body> <h1>Hello World</h1> <p>This markup was generated by Azion - Functions.</p> </body>`
async function handleRequest(request) { return new Response(html, { headers: { "content-type": "text/html;charset=UTF-8", }, }) }In this example, the response will be the HTML content, declared previously by the const html. The headers can be manipulated as well, and in the example, the content type is set.
Testing and Debugging
After writing your functions, you can preview the response and inspect the code. The preview simulates a request, and this simulation can be altered to meet the developer’s needs.
Learn more about Azion Preview Deployment.
It’s possible to debug the functions through:
Function instantiation
Once you’ve saved your function, it’s necessary to instantiate it in an applications.
Learn more on how to instantiate a function on Applications.