Though there are many, what are some practical uses?
In this article, we explore one potential use case, that of performing redirects directly on the edge.
Once you sign on to the CloudFlare Dashboard, you will notice an icon for Workers.
Let’s take a look at the development environment to understand how this can help us.
There are three main sections.
On the left side, we have a code editor and on the right, we have two views.
In the first section, the
is what first intercepts the incoming request.
That request is then sent to the
function for processing.
The core idea is that we will read thepathnameof the incoming request URL and decide where to redirect.
By using the URL throw in, we can easily map our incomingpathnameto a location.
By using the JavaScriptMapconstruct, we can create an easy to reference list of redirects.
Let’s move our redirects into aMapconstruct and refactor our code to make it work a bit better.
Deploying
Now that our script is functional, we need to map this to a domain.
choose the domain we want to apply this worker to, then navigate to the “Workers” tab.
grab the “Add Route” button, and you are able to define the script and route.
Under the “Route” entry, we need to define where the link will go.
Finally, we will opt for Worker that we have created to deploy to this zone.
This also means that changes, errors and all, take effect instantly as well.
Future Possibilities
CloudFlare has made several other tools and capabilities available using or integrating CloudFlare Workers.
There are many possibilities that this raises and what can be created using CloudFlare Workers.