0Pricing
Edge Computing with Cloudflare Workers & Deno · บทเรียน

การพัฒนาแอป Deno ในเครื่อง

สร้างและเรียกใช้แอปพลิเคชัน Deno อย่างง่ายในเครื่อง โดยเน้นการรับส่งข้อมูลกับไฟล์และคำขอเครือข่าย

การพัฒนาแอป Deno ในเครื่อง เป็นบทเรียน Edge Computing with Cloudflare Workers & Deno ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Edge Computing with Cloudflare Workers & Deno และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Edge Computing with Cloudflare Workers & Deno มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Your First Local Deno App

Welcome! In this lesson, we'll dive into building and running Deno applications right on your local machine.

Developing locally is crucial for testing and iterating quickly before deploying to the edge.

We'll focus on two fundamental operations: reading and writing files, and making network requests.

Hello, Deno!

Let's start with a classic "Hello, World!" program. Deno executes TypeScript or JavaScript directly.

Save this code as main.ts and run it using the deno run command in your terminal.

// main.ts
console.log("Hello from local Deno!");

Accessing Local Files

Deno has a secure permission model. To read files, you must explicitly grant permission using the --allow-read flag.

The Deno.readTextFile() function is an asynchronous way to read the content of a file as a string.

  • Deno.readTextFile(path): Reads a file.
  • --allow-read: Grants permission to read files.

Reading a Text File

Let's create a file named hello.txt with some content, then read it using Deno.

Remember to use the --allow-read flag when running this script!

// read_file.ts
// First, create a file named hello.txt with content like "Deno is awesome!"

async function readFileContent() {
  try {
    const filePath = "./hello.txt";
    const content = await Deno.readTextFile(filePath);
    console.log("File Content:", content);
  } catch (error) {
    console.error("Error reading file:", error.message);
  }
}

readFileContent();

Saving Data to Files

Just like reading, writing files also requires explicit permission: --allow-write.

The Deno.writeTextFile() function allows you to write a string to a file. It will create the file if it doesn't exist, or overwrite it if it does.

  • Deno.writeTextFile(path, data): Writes data to a file.
  • --allow-write: Grants permission to write files.

Writing to a Text File

Let's write some new content into a file. We'll create output.txt with a simple message.

Run this with deno run --allow-write write_file.ts.

// write_file.ts

async function writeFileContent() {
  try {
    const filePath = "./output.txt";
    const content = "This is new content written by Deno!";
    await Deno.writeTextFile(filePath, content);
    console.log("File written successfully to", filePath);
  } catch (error) {
    console.error("Error writing file:", error.message);
  }
}

writeFileContent();

Fetching Data from the Web

Deno includes a built-in fetch API, similar to browsers, for making HTTP requests.

To allow your Deno application to access the network, you need to use the --allow-net flag.

  • fetch(url): Initiates a network request.
  • --allow-net: Grants network access.

Getting Data from an API

Let's fetch some public data from a test API. We'll get a list of posts from JSONPlaceholder.

Run this with deno run --allow-net fetch_data.ts.

// fetch_data.ts

async function fetchData() {
  try {
    const response = await fetch("https://jsonplaceholder.typicode.com/posts/1");
    if (!response.ok) {
      throw new Error(`HTTP error! Status: ${response.status}`);
    }
    const data = await response.json();
    console.log("Fetched Data:", data);
  } catch (error) {
    console.error("Error fetching data:", error.message);
  }
}

fetchData();

Deno Permissions Check

Deno prioritizes security through explicit permissions. Which flag is needed to allow a Deno script to read a local file AND make an HTTP request?

Local Deno Apps Summary

Great job! You've learned how to build and run simple Deno applications locally.

  • We covered using deno run for executing scripts.
  • You practiced reading and writing files using Deno.readTextFile() and Deno.writeTextFile() with their respective --allow-read and --allow-write permissions.
  • You also fetched data from an external API using the fetch API and the --allow-net flag.

These are core skills for any Deno developer, setting you up for more complex applications, including those deployed to the edge!

คำถามที่พบบ่อย

บทเรียน “การพัฒนาแอป Deno ในเครื่อง” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การพัฒนาแอป Deno ในเครื่อง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Edge Computing with Cloudflare Workers & Deno ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Edge Computing with Cloudflare Workers & Deno มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การพัฒนาแอป Deno ในเครื่อง”

สร้างและเรียกใช้แอปพลิเคชัน Deno อย่างง่ายในเครื่อง โดยเน้นการรับส่งข้อมูลกับไฟล์และคำขอเครือข่าย คุณปฏิบัติ Edge Computing with Cloudflare Workers & Deno ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Edge Computing with Cloudflare Workers & Deno หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Edge Computing with Cloudflare Workers & Deno บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “การพัฒนาแอป Deno ในเครื่อง” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Edge Computing with Cloudflare Workers & Deno นี้ได้ไหม

ได้ บทเรียน Edge Computing with Cloudflare Workers & Deno ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. พื้นฐานสำคัญของ Deno Runtime
  2. ระบบโมดูลและสิทธิ์การอนุญาต
  3. การพัฒนาแอป Deno ในเครื่อง
  4. การทดสอบแอปพลิเคชัน Deno
← กลับไปที่ Edge Computing with Cloudflare Workers & Deno