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 v2+ is installed on your local machine:
$ wrangler version
If you see
wrangler v1....
, then you need to remove the old version:$ npm uninstall -g @cloudflare/wrangler
Now install if needed; note the missing "@cloudflare/":
$ npm install -g wrangler $ wrangler version ⛅️ wrangler 2.*.*
-
Authenticate with Cloudflare:
$ 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
- Updated all dependencies
- Add to
wrangler.toml
compatibility_date = "2023-01-20"
(CloudFlare's new versioning tag) - Switching from Wrangler V1 to Wrangler V2
- Initial version