快速搭建缩短链接 使用cloudflare workers和KV存储空间免费搭建 短链接服务

 使用cloudflare的workers加上kv的命名空间,实现缩短链接 个人主页,轻松缩短

使用cloudflare的workers加上kv的命名空間,實現縮短連結 個人主頁,輕鬆縮短
This video captures the use of Cloudflare's Workers plus KV's namespace to shorten the link
Short link building personal homepage, easy to shorten
使用cloudflare workers和KV免费搭建 短链接服务

GitHub代码地址:https://github.com/wandduse/workers_kv_ ... /tree/main
博客地址:https://3to1.xyz/2401
YouTube:https://youtu.be/DH0Hgt61Zg8
哔哩哔哩 (゜-゜)つロ 干杯~-bilibili:https://www.bilibili.com/video/BV1Ye411n77u/
-----------------------------------------------------------------------------------------------------------------------
下面是视频中用到的代码

代码

const html404 = `<!DOCTYPE html>
<body>
  <h1>404 Not Found.</h1>
</body>`

async function handleRequest(request) {
  const requestURL = new URL(request.url);
  const path = requestURL.pathname.split("/")[1];
  if(!path){
    //自定义页面
    const html= await fetch("https://xytom.github.io/Url-Shorten-Worker/index.html");
    //首页跳转到指定地址
    //return Response.redirect("https://www.wanuse.com", 302);
    //加载自定义页面
    return new Response(await html.text(), { headers: { "content-type": "text/html;charset=UTF-8", }, });
  }else{
  const value = await DDD.get(path);
  if (value) { return Response.redirect(value, 302)}
  return new Response(html404, {  headers: {   "content-type": "text/html;charset=UTF-8", }, status: 404 });
  }
}

addEventListener("fetch", async event => {
  event.respondWith(handleRequest(event.request))
});

DDD网络科技