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

模块系统与权限

了解 Deno 基于 URL 的模块系统及其细粒度权限模型,实现安全执行。

模块系统与权限 是 CoddyKit 上的免费 Edge Computing with Cloudflare Workers & Deno 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Edge Computing with Cloudflare Workers & Deno 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Edge Computing with Cloudflare Workers & Deno 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Deno Modules: Building Blocks

In Deno, applications are built using modules. Think of modules as individual files that contain related code, like functions, classes, or variables.

They help keep your code organized and reusable, preventing your project from becoming one giant, hard-to-manage file.

URLs as Module Paths

Unlike Node.js, Deno doesn't use a node_modules folder. Instead, Deno imports modules directly using URLs or file paths.

  • Local Files: Use relative or absolute paths (e.g., ./my_module.ts).
  • Remote Files: Use full URLs (e.g., https://deno.land/std/fs/mod.ts).

This simple approach makes dependency management very straightforward!

Importing Your Own Code

Let's create two files: utils.ts with a simple function, and main.ts to import and use it. This is how you share logic within your own project.

utils.ts:

export function greet(name: string): string {
  return `Hello, ${name}!`;
}

Running Local Module Example

Now, let's import and use our greet function in main.ts. Notice the relative path ./utils.ts.

Try running this example:

// main.ts
import { greet } from "./utils.ts";

console.log(greet("CoddyKit"));

Fetching Modules from the Web

Deno can directly import modules from web URLs. This is powerful, but also requires trust in the source. Deno caches these modules after the first download.

Here's how to import a utility from Deno's standard library:

// main.ts
import { join } from "https://deno.land/std/path/mod.ts";

const path = join("/users", "coddy", "documents");
console.log(`Joined path: ${path}`);

Deno Caches Remote Modules

When you first run a Deno script that imports remote modules, Deno downloads and caches them locally. This makes subsequent runs much faster.

  • Cached modules are stored in a global directory.
  • You can manually clear the cache with deno cache --reload or deno cache --reset.
  • Deno verifies module integrity using checksums.

Secure by Default: Permissions

Deno is designed to be secure. By default, a Deno program has no access to your file system, network, or environment variables.

You must explicitly grant these permissions using command-line flags when running your script. This prevents malicious code from doing harm without your knowledge.

Reading Files Securely

To allow your Deno program to read files, you need the --allow-read permission. You can specify specific paths or allow access to everything with . (current directory) or by omitting a path.

data.txt:

Hello Deno permissions!

`--allow-read` in Action

Let's try to read data.txt. Without permission, it will fail. With --allow-read, it works! (e.g., deno run --allow-read main.ts)

Try running this example:

// main.ts
const filePath = "./data.txt";
try {
  const content = await Deno.readTextFile(filePath);
  console.log("File content:", content);
} catch (error) {
  console.error("Error reading file:", error.message);
}

Making Network Requests

To allow your Deno program to make network requests (like fetching data from an API), you need the --allow-net permission.

You can specify specific hosts (e.g., --allow-net=api.example.com) or allow all network access. (e.g., deno run --allow-net main.ts)

// main.ts
try {
  const response = await fetch("https://deno.land/std/README.md");
  if (response.ok) {
    const text = await response.text();
    console.log("Fetched content (first 50 chars):");
    console.log(text.substring(0, 50));
  } else {
    console.error("Failed to fetch:", response.statusText);
  }
} catch (error) {
  console.error("Network error:", error.message);
}

Permission Challenge!

Which of the following Deno commands would allow a script named app.ts to both read files from the current directory AND make network requests to any domain?

Recap: Modules & Permissions

Great job! You've learned about Deno's modern module system, which uses URLs for both local and remote imports, simplifying dependency management.

You also explored Deno's robust permission model, understanding how to grant specific access (like --allow-read and --allow-net) to your scripts for secure execution. This "secure by default" approach is a core Deno feature!

常见问题解答

「模块系统与权限」课时是免费的吗?

是的 — 「模块系统与权限」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Edge Computing with Cloudflare Workers & Deno 课程的其余内容,请升级到 CoddyKit PRO。 Edge Computing with Cloudflare Workers & Deno 课程共包含 4 节课。

「模块系统与权限」这节课中我会学到什么?

了解 Deno 基于 URL 的模块系统及其细粒度权限模型,实现安全执行。 你通过在浏览器中直接运行的动手代码来练习 Edge Computing with Cloudflare Workers & Deno,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

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

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

「模块系统与权限」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Edge Computing with Cloudflare Workers & Deno 课中编写并运行代码吗?

能。每节 Edge Computing with Cloudflare Workers & Deno 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. Deno 运行时基础
  2. 模块系统与权限
  3. 开发本地 Deno 应用
  4. 测试 Deno 应用
← 返回 Edge Computing with Cloudflare Workers & Deno