0Pricing
Edge Computing with Cloudflare Workers & Deno · レッスン

Worker-Denoプロジェクトのセットアップ

ローカル開発にDeno、デプロイにCloudflare Workersを組み合わせたプロジェクトを設定します

「Worker-Denoプロジェクトのセットアップ」はCoddyKit上の無料Edge Computing with Cloudflare Workers & Denoレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはEdge Computing with Cloudflare Workers & Deno学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Edge Computing with Cloudflare Workers & Denoコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Deno & Workers: Best of Both

Welcome! In this lesson, we'll learn how to set up a project that uses Deno for local development and Cloudflare Workers for deployment to the edge.

This powerful combination lets you leverage the best features of both platforms.

Why Combine Deno & Workers?

Deno offers a fantastic local developer experience with its built-in tools for formatting, linting, and testing. Cloudflare Workers, on the other hand, provide a serverless platform to deploy your code globally, close to your users.

A combined setup allows you to:

  • Develop and test locally with Deno's robust tooling.
  • Deploy your application to Cloudflare's low-latency edge network.
  • Reuse core application logic across both environments.

Organizing Your Hybrid Project

A typical project structure for Deno and Workers involves separating entry points while sharing core logic. Here's a common layout:

  • src/: Contains your shared Deno-compatible logic.
  • worker-entry.ts: The main entry point for your Cloudflare Worker.
  • deno-local.ts: A script to run your application locally using Deno.
  • wrangler.toml: Cloudflare Worker configuration file.

This keeps things tidy and manageable.

Cloudflare Worker Setup

We'll assume you have the Wrangler CLI installed and configured from previous lessons. This tool is essential for developing and deploying Cloudflare Workers.

Your wrangler.toml file will define how your Worker builds and deploys. For Deno projects, it often points to a bundled JavaScript output file.

Basic Deno Project Init

For local Deno development, you simply create your .ts or .js files. Deno doesn't require a package.json or a node_modules folder like Node.js.

You can also use a deno.json file to configure project-specific settings, like tasks, formatting rules, and imports.

Writing Reusable Code

The key to a successful hybrid project is writing core application logic that can run in both Deno and Worker environments.

  • Use standard Web APIs (e.g., Request, Response, URL).
  • Avoid Node.js-specific APIs.
  • Place this shared logic in a common directory, like src/.

This ensures your code works seamlessly in both contexts.

Deno Local Server Example

Here's how you might set up a basic Deno server to run your shared logic locally. This allows for quick testing and iteration without deploying to the edge.

import { serve } from "https://deno.land/std/http/server.ts";

// This function would typically be imported from a shared 'src/' file.
function handleLocalRequest(request: Request): Response {
  const url = new URL(request.url);
  if (url.pathname === "/local-greet") {
    return new Response("Hello from Deno local server!", { status: 200 });
  }
  return new Response("Not Found", { status: 404 });
}

console.log("Deno server running on http://localhost:8000/");
serve(handleLocalRequest, { port: 8000 });

Cloudflare Worker Entry Point

Your Cloudflare Worker's entry point (e.g., worker-entry.ts) will import and use the same shared logic. Wrangler will then bundle this Deno-compatible code into a single JavaScript file for deployment.

export default {
  async fetch(request: Request): Promise<Response> {
    const url = new URL(request.url);
    // Imagine importing and using a shared function here
    // import { handleRequest } from './src/handler.ts';
    // return handleRequest(request);

    // For this example, we'll use a simplified inline logic
    if (url.pathname === "/edge-greet") {
      return new Response("Hello from Cloudflare Worker!", { status: 200 });
    }
    return new Response("Not Found", { status: 404 });
  },
};

Bundling Deno for Workers

Cloudflare Workers execute standard JavaScript. Since Deno often uses TypeScript and a URL-based module system, your Deno code needs to be transpiled (converted to JavaScript) and bundled into a single file.

Wrangler handles this process automatically if configured correctly, often using tools like esbuild internally. This ensures your Deno logic runs efficiently at the edge.

Project Setup Check

When setting up a project to use Deno for local development and Cloudflare Workers for deployment, what is a primary benefit of this hybrid approach?

Project Setup Recap

In this lesson, we explored how to structure a project that utilizes Deno for local development and Cloudflare Workers for edge deployment. We discussed the benefits of this approach, how to organize shared code, and saw examples of Deno local server and Worker entry points.

Next, we'll dive into implementing shared utility functions and business logic that can be reused across both environments.

よくある質問

「Worker-Denoプロジェクトのセットアップ」レッスンは無料ですか?

はい。「Worker-Denoプロジェクトのセットアップ」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Edge Computing with Cloudflare Workers & Denoコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Edge Computing with Cloudflare Workers & Denoコースには全4レッスンが含まれています。

「Worker-Denoプロジェクトのセットアップ」で何を学びますか?

ローカル開発にDeno、デプロイにCloudflare Workersを組み合わせたプロジェクトを設定します ブラウザで直接実行するハンズオンコードでEdge Computing with Cloudflare Workers & Denoを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Edge Computing with Cloudflare Workers & Denoを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのEdge Computing with Cloudflare Workers & Denoは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。

「Worker-Denoプロジェクトのセットアップ」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このEdge Computing with Cloudflare Workers & Denoレッスンでコードを書いて実行できますか?

はい。すべてのEdge Computing with Cloudflare Workers & Denoレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. Cloudflare WorkersでのDeno
  2. Worker-Denoプロジェクトのセットアップ
  3. 共有ロジックとユーティリティ
  4. 統合スタックのデプロイとデバッグ
← Edge Computing with Cloudflare Workers & Denoに戻る