0Pricing
Edge Computing with Cloudflare Workers & Deno · 课时

Cloudflare Workers 中的 Deno

探索如何将 Deno 模块转译或打包,以部署为 Cloudflare Workers

Cloudflare Workers 中的 Deno 是 CoddyKit 上的免费 Edge Computing with Cloudflare Workers & Deno 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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 导师)并解锁 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 导师会在你学习这节课的过程中回答你的问题。

学习 Edge Computing with Cloudflare Workers & Deno 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Edge Computing with Cloudflare Workers & Deno 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 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