This is a link shortener that runs as a Cloudflare Worker. It's the finished code for the Build a Link Shortener with Cloudflare Workers tutorial on DEV.to. To deploy it on your own Cloudflare account, follow these steps:
-
Clone this repo:
git clone https://github.com/mm/cf-shortener.git
-
Ensure Wrangler is installed on your local machine and authenticate with Cloudflare:
$ npm i @cloudflare/wrangler -g $ wrangler login
-
In the project directory, copy the
wrangler.example.toml
file towrangler.toml
. Get your Cloudflare Account ID by runningwrangler whoami
. Inwrangler.toml
, replaceACCOUNT_ID_HERE
with the account ID you get from Wrangler or the Workers dashboard. -
Create the Workers KV namespace for the shortener:
$ wrangler kv:namespace create "SHORTEN" $ wrangler kv:namespace create "SHORTEN" --preview
Copy the
id
you get in the terminal output towrangler.toml
in place ofID_HERE
, and copy thepreview_id
you get in the terminal output in place ofPREVIEW_ID_HERE
. -
Deploy the Worker:
wrangler publish