Alibaba Cloud Content Delivery Network (CDN) provides the EdgeRoutine CLI to run custom
JavaScript code on CDN nodes. This topic describes how to use the EdgeRoutine CLI
to deploy JavaScript code.
Prerequisites
The runtime environment of the EdgeRoutine CLI must be Node.js 8.0 or later.
To enable EdgeRoutine, you must map the serverless computing environment on CDN nodes
to your accelerated domain name. If you have not prepared the accelerated domain name
and AccessKey information for mapping, you must log on to the Alibaba Cloud CDN console
with your Alibaba Cloud account. In the console, you can add the domain name to Alibaba
Cloud CDN. For more information, see Add a domain name to Alibaba Cloud CDN. You can also view your AccessKey information including the AccessKey ID and AccessKey
secret in the console. For more information, see Create an AccessKey pair.
Procedure
- Install the EdgeRoutine CLI.
Open your terminal and run the following command to install the EdgeRoutine CLI:
$ npm install @alicloud/edgeroutine-cli -g
Note The latest version of the EdgeRoutine CLI is 2.0.3. After you install the EdgeRoutine
CLI, you can run the edgeroutine-cli -v
command to query the version number. We recommend that you upgrade your EdgeRoutine
CLI to the latest version. If you use the latest version of the EdgeRoutine CLI to
perform the build operation and the message "The specified ArgValue is invalid" appears,
run the edgeroutine-cli config
command to initialize the config.js file and perform the build operation again.
- Run the following command to create a code directory and change to the directory:
$ mkdir yourProject; cd yourProject
- Initialize the environment and create a code template.
Run the following command to initialize the environment:
$ edgeroutine-cli init
The following example shows a code template. If you want to use your own code, you
can move your code file to the code directory that you have created and rename the
code file
edge.js.
/**
* Add the necessary event listener
* @param {Event} fetch event, {Function} async function
*/
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
/**
* Make a response to client
* @param {Request} request
*/
async function handleRequest(request) {
return new Response('Hello World!', { status: 200 })
}
- Configure the accelerated domain name and the AccessKey pair, including the AccessKey
ID and AccessKey secret.
- Deploy the code to the staging environment.
Note Before you deploy the code to the production environment, you must validate the code
in the staging environment.
- Build the code in the staging environment.
- View the code in the staging environment.
$ edgeroutine-cli build --show
- Delete the code in the staging environment.
This operation deletes configurations from the staging environment. Proceed with caution.
$ edgeroutine-cli build --delete
- Validate the code in the staging environment.
You must connect to the accelerated domain name in the terminal environment by using
the IP address of the staging environment for EdgeRoutine. The IP address is 42.123.119.50
or 42.123.119.51.
$ curl-v'http://yourdomain.com/yourpath/'-x42.123.119.50:80
- Deploy the code to the production environment.
- After you validate the code in the staging environment, you can deploy the code to
the production environment.
$ edgeroutine-cli publish
Notice EdgeScript and EdgeRoutine use the same deployment system. If you use
EdgeScript and EdgeRoutine at the same time, the
edgeroutine-cli publish
command that is run in the EdgeRoutine CLI will deploy scripts of both EdgeScript
and EdgeRoutine from the staging environment to the production environment. Before
you deploy code, you can run the
edgeroutine-cli build -s
command to query scripts of EdgeScript and EdgeRoutine that are in the staging environment.
After you confirm the configurations that you want to deploy to the production environment,
run the edgeroutine-cli publish commend to deploy the configurations.
- View the code in the production environment.
$ edgeroutine-cli publish --show
- Delete the code in the production environment. Then, the code and relevant configurations
are deleted. This step is optional.
$ edgeroutine-cli publish --delete
- Run tests in the production environment.
You can test the workloads of the accelerated domain name in the production environment.
$ curl -v 'https://yourdomain.com/yourpath/'