Free URL shortener with Cloudflare Workers

const redirects = new Map([
["cloudflare-shortener", "https://lucjan.medium.com/free-url-shortener-with-cloudflare-workers-125eaf87b1ec"],
["neil-pattel-copied-my-design", "https://lucjan.medium.com/how-neil-patel-copied-my-design-and-im-not-even-mad-ede6e99648c0"],
["surfer", "https://surferseo.com"],
])
addEventListener('fetch', event => {
event.respondWith(handleRedirect(event.request));
})
async function handleRedirect(request) {
let pathname = new URL(request.url).pathname.replace("/", "");
let location = redirects.get(pathname);
return location
? Response.redirect(location, 301)
: new Response("Not Found", {status: 404})
}

How to get it working

--

--

--

Co-founder and CTO at Surfer. Loves building web products.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Web Technologies I will learn in 2021

BeanShell Processor Tutorial 1: Basic Usage

(Commercial Off-the-shelf) COTS vs Custom Development

Server Activity

Clean Architecture .NET Core (Part 1: Introduction)

restQL V3 is here

Advent of Spreadsheets: Solving coding puzzles in plain sight (Advent of Code 2018 Days 1–5)

Host WordPress on your Win 11 / 10 with Free Docker Desktop

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Lucjan Suski

Lucjan Suski

Co-founder and CTO at Surfer. Loves building web products.

More from Medium

How sniping bots stole $2M

Cash App Failed for My Protection | cashappreviews | 2022 |

Hipo: Innovative Loan Model On DeFi