Deno 运行时基础
探索 Deno 的核心功能和内置工具,并了解它与 Node.js 的区别。
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 节课。
本课时的部分内容尚未翻译,以英文显示。
Welcome to Deno Essentials
Welcome! In this lesson, we'll dive into Deno, a modern, secure runtime for JavaScript and TypeScript.
Created by Ryan Dahl (who also created Node.js), Deno addresses some of the design choices made in Node.js, focusing on security and a great developer experience.
Security First: Deno's Approach
One of Deno's biggest differences is its security model. By default, Deno scripts run in a sandbox, meaning they have no access to:
- The file system
- The network
- Environment variables
- Subprocesses
You must explicitly grant these permissions using command-line flags. This prevents malicious code from doing harm without your knowledge.
Built-in Tooling
Deno comes with a powerful set of built-in tools. Forget installing separate packages for formatting, linting, or testing!
Key tools include:
deno fmt: Code formatterdeno lint: Linter for code qualitydeno test: Test runnerdeno doc: Documentation generatordeno compile: Compiles scripts into self-contained executables
This integrated experience simplifies project setup and maintenance.
Native TypeScript Support
Deno supports TypeScript out of the box. This means you can write TypeScript code without needing a separate compilation step or tsconfig.json file.
It also embraces ES Modules, allowing you to import modules directly from URLs, much like how browsers work. No more node_modules folder!
Your First Deno Program
Let's write a simple Deno program. Save this as hello.ts (or .js) and run it with deno run hello.ts.
console.log("Hello from Deno!");
console.log("Deno is awesome.");Deno vs. Node.js: Core Differences
While both Deno and Node.js are JavaScript runtimes, they have fundamental differences:
- Security: Deno is secure by default (permissions needed), Node.js has full access.
- TypeScript: Deno supports TS natively, Node.js requires transpilation.
- Tooling: Deno has built-in tools (fmt, lint, test), Node.js relies on npm packages.
- Module System: Deno uses URL-based ES Modules, Node.js uses CommonJS (though ES Modules are gaining traction).
- Standard Library: Deno has a robust standard library, Node.js has a smaller core.
The Global Deno Object
Deno exposes a global Deno object that provides access to runtime-specific APIs, like file system operations, network access, and environment variables.
Here's how you can access command-line arguments passed to your script:
console.log("Arguments:", Deno.args);
// Try running: deno run my_script.ts arg1 arg2Running with Permissions
To demonstrate Deno's permission model, let's try to read a file. This script will fail without explicit permission.
To run it successfully, you'll need to add the --allow-read flag: deno run --allow-read file_reader.ts
try {
const content = Deno.readTextFileSync("data.txt");
console.log("File content:\n" + content);
} catch (error) {
console.error("Error reading file: ", error.message);
console.error("Hint: Try 'deno run --allow-read file_reader.ts'");
}Deno Essentials Check
Which of the following statements about Deno is TRUE?
Recap: Deno's Foundation
Great job! In this lesson, you've learned about Deno's foundational features:
- Its security-first approach with a granular permission model.
- The convenience of its built-in tooling.
- Native TypeScript and ES Module support.
- The global
Denoobject for runtime interactions.
These essentials make Deno a powerful and modern runtime for web development, especially at the edge!
常见问题解答
「Deno 运行时基础」课时是免费的吗?
是的 — 「Deno 运行时基础」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Edge Computing with Cloudflare Workers & Deno 课程的其余内容,请升级到 CoddyKit PRO。 Edge Computing with Cloudflare Workers & Deno 课程共包含 4 节课。
「Deno 运行时基础」这节课中我会学到什么?
探索 Deno 的核心功能和内置工具,并了解它与 Node.js 的区别。 你通过在浏览器中直接运行的动手代码来练习 Edge Computing with Cloudflare Workers & Deno,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Edge Computing with Cloudflare Workers & Deno 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Edge Computing with Cloudflare Workers & Deno 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「Deno 运行时基础」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Edge Computing with Cloudflare Workers & Deno 课中编写并运行代码吗?
能。每节 Edge Computing with Cloudflare Workers & Deno 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- Deno 运行时基础
- 模块系统与权限
- 开发本地 Deno 应用
- 测试 Deno 应用