Edge Developer Platform
  • Edge Functions
    • Overview
    • Getting Started
    • Operation Guide
      • Function Management
      • Function Trigger
    • Runtime APIs
      • addEventListener
      • Cache
      • Cookies
      • Encoding
      • Fetch
      • FetchEvent
      • Headers
      • Request
      • Response
      • Streams
        • ReadableStream
        • ReadableStreamBYOBReader
        • ReadableStreamDefaultReader
        • TransformStream
        • WritableStream
        • WritableStreamDefaultWriter
      • Web Crypto
      • Web standards
      • Images
        • ImageProperties
    • Sample Functions
      • Returning an HTML Page
      • Returning a JSON Object
      • Fetch Remote Resources
      • Authenticating a Request Header
      • Modifying a Response Header
      • Performing an A/B Test
      • Setting Cookies
      • Performing Redirect Based on the Request Location
      • Using the Cache API
      • Caching POST Requests
      • Responding in Streaming Mode
      • Merging Resources and Responding in Streaming Mode
      • Protecting Data from Tampering
      • Rewriting a m3u8 File and Configuring Authentication
      • Adaptive Image Resize
      • Image Adaptive WebP
      • Customize Referer restriction rules
      • Remote Authentication
      • HMAC Digital Signature
      • Naming a Downloaded File
      • Obtaining Client IP Address
    • Best Practices
      • Adaptive Image Format Conversion via Edge Functions

Getting Started

This document describes how to create a simple edge function and use the function to redirect a request to another URL and return the custom response header.

Overview

The site example.com is currently connected to EdgeOne service. Under this site, a custom HTML activity page needs to be provided to the users through the custom domain name www.example.com. This page can be deployed to the edge nodes of EdgeOne's global AZs through the edge function for users to access nearby.
Note:
1. How to connect to the site, please refer to Quick Start.
2. How to add an acceleration domain, please refer to Adding a Domain Name for Acceleration.

Directions

Step 1: Creating and Deploying a Function

1. Log in to the EdgeOne Console and click Site List in the left sidebar. In the site list, click the Site to be configured.
2. On the site details page, click Edge Function > Function Management.
3. On the edge function management page, click Create function and select to create a function using a template. At this step, you can use a template to create a function according to actual business needs. Taking the current scenario as an example, you can select the "Create Hello World" template to create a new one. After selecting the template, click Next.
4. On the new Edge Functions page, configure the relevant parameters. The explanations for these parameters are as follows:
Function: Required, it can only contain letters, numbers, hyphens. It should start with a letter and end with a number or letter, 2-30 characters; and it cannot be modified after creation. For example: test-edgefunctions.
Description: Optional, it supports up to 60 characters. For Example: Custom HTML page and response header.
Code: the code corresponding to the template.
5. Click Create and deploy. If you see the following pop-up dialog box, the deployment is successful.


Step 2. Configuring a Triggering Rule

If you want to trigger function execution by setting HOST, URL Path, or file extensions of the matching sites, you could proceed in the following two steps:
1. After the function is deployed successfully, click Add triggering rule.
2. On the add triggering rule page, configure the matching conditions. Taking the current scenario as an example, you can select the Matching type as "HOST", the Operator as "Is", and the value as the added subdomain www.example.com. Click OK to create the trigger rule.


Step 3: Verifying the Edge Function

To check whether the function is working as expected, you can initiate a request in a browser or by using Curl.
Browser Verification
Curl Verification
Enter the URL in the browser, for instance:https://www.example.com/test-edgefunctions. This URL can match the set trigger conditions to trigger the execution of the function and view the response page information.

Run the curl request command within the MAC/Linux terminal to verify. For example:curl https://www.example.com/test-edgefunctions. The response can be viewed as follows: