该介绍目前仅提供英文版本。
A simple, serverless URL shortener built with Cloudflare Workers
Overview
URL Shortener Worker is a lightweight tool that allows you to create your own URL shortening service without managing any server infrastructure. By leveraging Cloudflare Workers and KV storage, this project provides a fast, reliable way to generate and manage shortened URLs.
Demo: https://lnks.eu.org/
Key Features
- ☁️ Serverless Architecture: Built on Cloudflare Workers - no servers to maintain
- ⚡ High Performance: Cloudflare’s global network ensures fast redirects worldwide
- 🔒 Free and Secure: Uses Cloudflare’s free tier with secure redirects
- 🧰 Easy Setup: Simple deployment with minimal configuration
- 📊 KV Storage: Efficient key-value storage for URL mappings
- 🌐 Custom Domain Support: Use your own domain for shortened links
Quick Start Guide
Setting up your own URL shortener is straightforward:
1. Create a KV Namespace
First, go to the Cloudflare dashboard and create a new KV namespace in the Workers KV section.
2. Bind the KV Namespace to Your Worker
In your Worker’s Settings tab:
- Add a KV Namespace binding
- Set the Variable name to
LINKS - Select the KV namespace you created in step 1
3. Deploy the Code
Copy the index.js code from this project to your Cloudflare Worker.
4. Save and Deploy
Click “Save and Deploy” to make your URL shortener live.
How It Works
The URL Shortener Worker operates through a simple yet efficient process:
- When a user submits a URL to be shortened, the Worker generates a unique short code
- The original URL and short code are stored in Cloudflare’s KV storage
- When someone visits the shortened URL, the Worker retrieves the original URL from KV storage
- The visitor is instantly redirected to the original URL
This process happens entirely on Cloudflare’s edge network, ensuring minimal latency regardless of where your users are located.
API Documentation
The URL Shortener provides a simple API for programmatic use:
- Shorten a URL: Send a POST request with the long URL
- Access a shortened URL: Simply visit the shortened link to be redirected
Detailed API documentation is available in the docs folder.
Advanced Features
Modified Version by crazypeace
There’s an enhanced version that includes:
- Custom short link creation
- Page caching for previously set short links
- localStorage search for long URLs
- Ability to delete specific short links
- Password protection for links
Implementation Notes
- The demo site automatically expires links after 24 hours to prevent abuse
- For long-term use, deploy your own instance
- The entire application consists of just one JavaScript file, making it easy to maintain
- No database server required - everything runs on Cloudflare’s infrastructure
Requirements
- Cloudflare account (free tier is sufficient)
- Basic knowledge of deploying Cloudflare Workers
License
This project is licensed under the MIT License - see the LICENSE file for details.