ระบบโมดูลและสิทธิ์การอนุญาต
ทำความเข้าใจระบบโมดูลที่อิงตาม URL ของ Deno และโมเดลสิทธิ์แบบละเอียดเพื่อการทำงานที่ปลอดภัย
ระบบโมดูลและสิทธิ์การอนุญาต เป็นบทเรียน Edge Computing with Cloudflare Workers & Deno ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 --reloadordeno 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 ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Edge Computing with Cloudflare Workers & Deno ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Edge Computing with Cloudflare Workers & Deno มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ระบบโมดูลและสิทธิ์การอนุญาต”
ทำความเข้าใจระบบโมดูลที่อิงตาม URL ของ Deno และโมเดลสิทธิ์แบบละเอียดเพื่อการทำงานที่ปลอดภัย คุณปฏิบัติ Edge Computing with Cloudflare Workers & Deno ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Edge Computing with Cloudflare Workers & Deno หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Edge Computing with Cloudflare Workers & Deno บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “ระบบโมดูลและสิทธิ์การอนุญาต” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Edge Computing with Cloudflare Workers & Deno นี้ได้ไหม
ได้ บทเรียน Edge Computing with Cloudflare Workers & Deno ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- พื้นฐานสำคัญของ Deno Runtime
- ระบบโมดูลและสิทธิ์การอนุญาต
- การพัฒนาแอป Deno ในเครื่อง
- การทดสอบแอปพลิเคชัน Deno