0Pricing
Edge Computing with Cloudflare Workers & Deno · บทเรียน

Cloudflare Workers บน Deno

สำรวจวิธีแปลงหรือรวมโมดูล Deno เพื่อใช้งานเป็น Cloudflare Workers

Cloudflare Workers บน Deno เป็นบทเรียน Edge Computing with Cloudflare Workers & Deno ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Edge Computing with Cloudflare Workers & Deno และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Edge Computing with Cloudflare Workers & Deno มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Deno on Cloudflare Workers

Welcome! In this lesson, we'll explore how to bring the power of Deno to Cloudflare Workers. This integration lets you leverage Deno's modern runtime features while benefiting from Workers' global edge network.

You'll learn how to prepare your Deno code so it can run seamlessly on Cloudflare's serverless platform.

Edge Runtime Differences

Cloudflare Workers run in a V8 JavaScript engine environment. While Deno also uses V8, it adds its own APIs (like Deno.readTextFile, Deno.serve) and a secure permission model.

These Deno-specific APIs aren't directly available in the standard Worker runtime. So, how do we make Deno code compatible?

Solution 1: Transpilation

The first key step is transpilation. This is the process of converting source code from one language or version to another while maintaining its functionality.

  • TypeScript to JavaScript: Deno often uses TypeScript. Workers need plain JavaScript. Transpilation converts your .ts files to .js.
  • Modern JS to Older JS: Sometimes, newer JavaScript features might be transpiled down for broader compatibility.

TypeScript Transpilation

Imagine you have a Deno utility written in TypeScript:

// my_util.ts
export function calculateSum(a: number, b: number): number {
  return a + b;
}

During transpilation, this becomes standard JavaScript, removing type annotations:

// my_util.js (after transpilation)
export function calculateSum(a, b) {
  return a + b;
}

This JavaScript is now understood by the Worker runtime.

Solution 2: Bundling

The second crucial step is bundling. This combines multiple JavaScript modules and their dependencies into a single, optimized JavaScript file.

  • Single File: Workers are often deployed as a single JavaScript file.
  • Performance: Bundling reduces the number of files the runtime needs to load, improving startup time.
  • Dependency Management: It resolves and includes all necessary code from imported modules.

Deno Utility Function

Here's a simple Deno function. In a real project, this might be in its own module (e.g., utils.ts) and imported. For now, let's see it run directly:

function getGreeting(name: string): string {
  return `Hello, ${name}! Welcome to the Edge!`;
}

console.log(getGreeting("CoddyKit Learner"));

The Bundling Process

Imagine we have our getGreeting function from the previous scene, perhaps in a file called src/utils.ts, and our main Worker logic in src/worker.ts that imports it.

// src/worker.ts
import { getGreeting } from './utils.ts';

// ... Worker logic ...

A bundler will take src/worker.ts as an entry point, resolve the import, transpile any TypeScript, and combine everything into one JavaScript file.

Deno-Powered Worker Output

After bundling, your Deno code, now transpiled to JavaScript and combined into a single file, looks like a standard Cloudflare Worker. This is the file you'd deploy.

Here's a simplified example of what the output might resemble, including our greeting logic:

// worker.js (bundled output)
function getGreeting(name) {
  return `Hello, ${name}! Welcome to the Edge!`;
}

export default {
  async fetch(request) {
    const url = new URL(request.url);
    const name = url.searchParams.get("name") || "Guest";
    const message = getGreeting(name);

    return new Response(message, {
      headers: { "content-type": "text/plain" },
    });
  },
};

Deployment with Wrangler

Once your Deno code is transpiled and bundled into a single JavaScript file (e.g., worker.js), you can deploy it using Cloudflare's Wrangler CLI, just like any other Worker.

  • Wrangler handles uploading this bundled file to Cloudflare.
  • The Cloudflare edge network then runs your JavaScript, effectively executing your Deno-originating logic.

The developer experience is Deno-first, but the deployment target is a standard Worker.

Quick Check

When deploying Deno code to Cloudflare Workers, which two processes are essential to make your Deno modules compatible with the Worker runtime?

Recap: Deno at the Edge

Great job! You've learned how Deno modules can be deployed as Cloudflare Workers.

  • Cloudflare Workers run JavaScript, so Deno code needs transpilation (e.g., TypeScript to JavaScript).
  • For efficient deployment, multiple Deno modules are combined into a single file through bundling.
  • Tools like esbuild automate this, turning your Deno project into a standard Worker script.

This allows you to leverage Deno's ecosystem for development while benefiting from the global reach of Cloudflare Workers.

คำถามที่พบบ่อย

บทเรียน “Cloudflare Workers บน Deno” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “Cloudflare Workers บน Deno” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Edge Computing with Cloudflare Workers & Deno ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Edge Computing with Cloudflare Workers & Deno มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “Cloudflare Workers บน Deno”

สำรวจวิธีแปลงหรือรวมโมดูล Deno เพื่อใช้งานเป็น Cloudflare Workers คุณปฏิบัติ Edge Computing with Cloudflare Workers & Deno ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Edge Computing with Cloudflare Workers & Deno หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Edge Computing with Cloudflare Workers & Deno บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “Cloudflare Workers บน Deno” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Edge Computing with Cloudflare Workers & Deno นี้ได้ไหม

ได้ บทเรียน Edge Computing with Cloudflare Workers & Deno ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. Cloudflare Workers บน Deno
  2. การตั้งค่าโครงการ Worker-Deno
  3. ตรรกะร่วมและเครื่องมืออรรถประโยชน์
  4. นำสแตกที่ผสานรวมไปใช้งานและแก้ไขข้อบกพร่อง
← กลับไปที่ Edge Computing with Cloudflare Workers & Deno