0Pricing
Edge Computing with Cloudflare Workers & Deno · 강의

모듈 시스템 및 권한

Deno의 URL 기반 모듈 시스템과 안전한 실행을 위한 세분화된 권한 모델을 이해합니다.

모듈 시스템 및 권한은(는) CoddyKit의 무료 Edge Computing with Cloudflare Workers & Deno 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 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!

자주 묻는 질문

“모듈 시스템 및 권한” 강의는 무료인가요?

네 — “모듈 시스템 및 권한” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Edge Computing with Cloudflare Workers & Deno 강의 전체를 잠금 해제할 수 있습니다. Edge Computing with Cloudflare Workers & Deno 강의에는 총 4개의 강의가 포함되어 있습니다.

“모듈 시스템 및 권한”에서 뭘 배우나요?

Deno의 URL 기반 모듈 시스템과 안전한 실행을 위한 세분화된 권한 모델을 이해합니다. 브라우저에서 직접 실행하는 실습 코드로 Edge Computing with Cloudflare Workers & Deno을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Edge Computing with Cloudflare Workers & Deno을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Edge Computing with Cloudflare Workers & Deno은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.

“모듈 시스템 및 권한” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Edge Computing with Cloudflare Workers & Deno 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Edge Computing with Cloudflare Workers & Deno 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. Deno 런타임 핵심
  2. 모듈 시스템 및 권한
  3. 로컬 Deno 앱 개발하기
  4. 데노 애플리케이션 테스트
← Edge Computing with Cloudflare Workers & Deno(으)로 돌아가기