0Pricing
Edge Computing with Cloudflare Workers & Deno · Lezione

Sistema di moduli e autorizzazioni

Comprenda il sistema di moduli basato su URL di Deno e il suo modello granulare di autorizzazioni per un'esecuzione sicura.

Sistema di moduli e autorizzazioni è una lezione Edge Computing with Cloudflare Workers & Deno gratuita su CoddyKit. Questa è la lezione 2 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Edge Computing with Cloudflare Workers & Deno, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Edge Computing with Cloudflare Workers & Deno include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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!

Domande Frequenti

La lezione «Sistema di moduli e autorizzazioni» è gratuita?

Sì — il testo completo di «Sistema di moduli e autorizzazioni» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Edge Computing with Cloudflare Workers & Deno, passa a CoddyKit PRO. Il corso Edge Computing with Cloudflare Workers & Deno include 4 lezioni in totale.

Cosa imparerò in «Sistema di moduli e autorizzazioni»?

Comprenda il sistema di moduli basato su URL di Deno e il suo modello granulare di autorizzazioni per un'esecuzione sicura. Eserciti Edge Computing with Cloudflare Workers & Deno con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Edge Computing with Cloudflare Workers & Deno?

Non è richiesta alcuna esperienza precedente. Edge Computing with Cloudflare Workers & Deno su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 2 di 4.

Quanto tempo richiede la lezione «Sistema di moduli e autorizzazioni»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Edge Computing with Cloudflare Workers & Deno?

Sì. Ogni lezione Edge Computing with Cloudflare Workers & Deno include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Elementi essenziali del runtime Deno
  2. Sistema di moduli e autorizzazioni
  3. Sviluppare app Deno locali
  4. Testare le applicazioni Deno
← Torna a Edge Computing with Cloudflare Workers & Deno