/CloudflareWorker-KV-UrlShort

使用Cloudflare Worker创建的URL缩短器|支持自定义首页|支持Menu Short|支持短网址、文本、网页分享 URL Shortener created with Cloudflare Worker | Supports custom homepage | Supports Menu Short | Supports shortening URLs, text, and webpage sharing.

Primary LanguageJavaScriptMIT LicenseMIT

CloudflareWorkerKV-UrlShort

URL shortener created using Cloudflare Worker | Supports custom homepage | Supports Menu Short | Supports short URLs, text, web page sharing, ephemeral content, expiration time, and UI adaptive interface.

Since the free plan of Cloudflare has limitations, a preview URL is not provided. Please visit Cloudflare Work for deployment.

Normally, the free plan is sufficient for personal use, but you can upgrade to a paid plan if you have high demand.

Workers

Each request uses a maximum of 10 milliseconds of CPU time. The minimum latency after the first request. Up to 100,000 requests per day (UTC+0).

KV

Globally low-latency key-value edge storage. Up to 100,000 read operations per day. Up to 1,000 write, delete, and list operations per day.

Preview Image

Preview Image

Constant Settings

// Admin page path (default is "/", can be set to another path like "/admin" if hiding the homepage)
const ADMIN_PATH = "/";
// API path
const API_PATH = "/api";
// Long URL key name
const URL_KEY = "longUrl";
// Short URL key name
const URL_NAME = "shortCode";
// Short URL key name (used for API response)
const SHORT_URL_KEY = "shorturl";
// Static homepage source code link (set to replace the homepage, can be commented out if not needed)
const STATICHTML = "https://github.com/Aiayw/CloudflareWorkerKV-UrlShort/blob/8eb8866c8acfd4ed130dc94f4a2798a78c898ecb/404.html";

Response Example

{
 "shortCode":"dfcw",
 "type":"link",
 "shorturl":"https://url.com/dfcw"
}

Custom Homepage

const statichtml = "https://github.com/Aiayw/CloudflareWorkerKV-UrlShort/blob/8eb8866c8acfd4ed130dc94f4a2798a78c898ecb/404.html"

Modify the above URL to the source code of your preferred web page. Only a single HTML file is supported. It is recommended to integrate CSS, JS, and scripts into the HTML file.


Create a Namespace in Workers KV

Create a Namespace in Workers KV

Bind the KV Namespace in the Worker's Settings Tab

Bind KV Namespace 1

Bind KV Namespace 2

Set the variable name as shortlink and fill in the KV Namespace with the one you created

Bind KV

Copy the code from index.js in this project and save and deploy it in Cloudflare Worker

Save and Deploy

Acknowledgments

Thanks to Cloudflare for providing the platform and services.

中文描述


CloudflareWorkerKV-UrlShort

使用Cloudflare Worker创建的URL缩短器|支持自定义首页|支持Menu Short|支持短网址、文本、网页分享、阅后即焚、过期时间、以及界面UI自适应。

因为Cloudflare的免费套餐有限制,所以不提供预览地址,请自行前往cloudflare Work部署。

正常来说免费套餐私人使用是完全够的,如有大量需求可付费升级套餐。

Workers

每个请求最多占用 10 毫秒 CPU 时间
第一个请求后的延迟最低
每天最多 100,000 个请求 (UTC+0)

KV

全局性的低延迟键值边缘存储
每天最多 100,000 次读取操作
每天最多 1,000 次写入、删除和列出操作

预览图

预览图

常量设置

// 管理员页面路径 (默认为 / 如果隐藏首页可设置为其他路径,例如:/admin )
const ADMIN_PATH = "/";
// API 路径
const API_PATH = "/api";
// 长链接键名
const URL_KEY = "longUrl";
// 短链接键名
const URL_NAME = "shortCode";
// 短链接键名(用于 API 返回)
const SHORT_URL_KEY = "shorturl";
// 静态首页源码链接 (设置首页替换页面,不需要也可以直接注释掉)
const STATICHTML = "https://github.com/Aiayw/CloudflareWorkerKV-UrlShort/blob/8eb8866c8acfd4ed130dc94f4a2798a78c898ecb/404.html";

响应参考

{
 "shortCode":"dfcw",
 "type":"link",
 "shorturl":"https://url.com/dfcw"
}

自定义首页

const statichtml = "https://github.com/Aiayw/CloudflareWorkerKV-UrlShort/blob/8eb8866c8acfd4ed130dc94f4a2798a78c898ecb/404.html"

修改上方网址为自己喜欢的网页源码,只支持单html,建议css和js和script集成进html里


去Workers KV中创建一个命名空间

去Workers KV中创建一个命名空间

去Worker的设置选选项卡中绑定KV 命名空间

绑定KV Namespace 1

绑定KV Namespace 2

其中变量名称填写shortlink, KV 命名空间填写你刚刚创建的命名空间

绑定kv

复制本项目中的index.js的代码到Cloudflare Worker 点击保存并部署

点击保存并部署

感谢

感谢Cloudflare提供平台和服务。