ESA points of presence (POPs) provide a serverless environment to run your code. You can write your code and configure trigger rules without needing to configure servers or manage infrastructure. Your code can scale elastically and run securely on POPs that are close to your users. This significantly reduces O&M costs and improves response speed.
Step 1: Create a function
Log on to the ESA console. In the navigation pane on the left, choose .
On the Functions and Pages page, click Create.
Click the Function Templates tab, select a function template as needed, and then click Next.

Enter a Routine Name and Description, preview the code details, and click Submit.

After the system build is complete, a public domain name is generated for you to access the function. You can use this domain name to directly access and preview the result. To bind a custom domain name, see How to bind a custom domain name.


Step 2: Develop and test the function
You can modify the function code and test it locally as needed. After that, deploy the code to the test environment. Continuously monitor its running status and performance to ensure stability and correctness.
Click Associate Domain Name and then select the Code tab. You can develop the function using
JavaScript ES6syntax.

ESA provides a staging environment to the right of the code development tool in the console. After you finish developing the code, click Save. In the toolbar on the right, construct the HTTP request method, request header, and request body. After the construction is complete, click the Request button. The console directly returns the response after the request is processed by the function.
After testing the code, click Generate Version.
Select the Deploy tab and click Release. In the dialog box that appears, select Staging Environment to publish the version. The function will take effect on the edge test POPs. You can directly bind a
Hostto send requests from a real client and view the results on the client.
Step 3: Publish the official version
After the function runs stably in the test environment and passes verification, you can publish the function code to the production environment.
Select the Deploy tab and click Released Versions .
Click Production Environment, select the version to be published, and then click Release. All POPs in the production environment will then deploy this version of the function code.

How to bind a custom domain name
You can bind a domain name to a function to forward all traffic from the specified domain name to that function. For example, if you bind er.example.com to a function, all requests to that domain name are handled by the function. You can also configure URL path rules through function routing to forward traffic from specific paths under a domain name to the function. For example, you can route requests for er.example.com/test/path/ to the function to achieve flexible and fine-grained traffic rerouting.
Prerequisites
Make sure that you have added the target website on ESA and the site status is active.
To access the edge function over HTTPS, make sure that a valid SSL/TLS certificate is configured.
Binding steps
On the Functions and Pages page, select the function that you created.

Select the Domain Names tab. You can forward traffic from the domain name to the function by adding a domain name or a routing rule.
Add a domain name
After you bind a domain name to the function, you can directly use that domain name to access the function.
For example, if you have added the site
example.comin ESA, you can bind the domain name aser.example.com. Then, requests toer.example.com,er.example.com/user, ander.example.com/loginwill be forwarded to the edge function.In the Custom Domains section, click Add Domain Name.
Enter the domain name and click OK.

NoteFor domain names connected using NS records: After you bind the domain name, the system automatically adds a DNS record for the function.
For domain names connected using a CNAME record: After you bind the domain name, a CNAME value is automatically generated. You must manually add a CNAME record with your domain name resolution service provider for the current site.
Add a route
The routing feature lets you map specific URLs to a function. When a request matches a configured URL, the function processes the request. Otherwise, the request continues through the accelerated back-to-origin ESA flow. As shown in the following figure, if you configure a routing rule of
example.com/a*for theexample.comsite, all path accesses that match the routing rule, such as/a,/a1, and/a2, are processed by the function. Other path accesses that do not match the routing rule, such as/b,/c, and/d, continue to follow the accelerated back-to-origin or caching flow.In the Routes section, click Add Route.
Enter a Route Name and select the target site from the Select Website list, for example,
example.com.Select a Route Mode.
Simple mode
Enter a Route Name and select the target site.
Select SImple Mode and enter the routing rule.

Custom mode
Enter a Route Name and select the target site.
Select Custom Mode.
In the If requests match... section, set the request features to match. For more information about how to configure rules, see Rule expression components.
Then, execute the edge function.

Example 1: If you set the route to
*.example.com/*, any request sent tohttp://www.example.com/orhttp://example.com/is forwarded to the function.Example 2: If you set the route to
example.com/a*, any request sent tohttp://example.com/a,http://example.com/a1, orhttp://example.com/apiis forwarded to the function.Example 3: If you set the route to
www.example.com/api/*, any request sent tohttp://www.example.com/api/and its subpaths, such ashttp://www.example.com/api/usersorhttp://www.example.com/api/products/123, is forwarded to the function.
Click OK.
NoteIf the route you enter is a domain name with a prefix, such as
*.example.comorwww.example.com, you must also manually add a record to the DNS records in ESA. Otherwise, access will fail.If multiple function routes are configured, the system checks them from top to bottom. When a match is found, the system stops checking the remaining routes.
Matching rules
A route configuration must include both the domain hostname and the path URI. Therefore, you cannot configure a routing rule that contains only a path URI, such as
/path.You can add a wildcard character (
*) at the beginning or end of a route configuration to match more requests. The wildcard character*can match zero or more of any character. For example,example.com/*matches all requests toexample.com.Route configurations are case-sensitive. For example,
example.com/aandexample.com/Aare two different routing rules.You cannot configure a wildcard character (
*) or parameters in the middle of a route. For example, formats such asexample.com/*/pathandexample.com/path?param=1are not allowed.When an access request matches multiple route configurations, the rule that was configured earlier takes precedence.