Worker-Deno 项目设置
配置一个结合 Deno 本地开发与 Cloudflare Workers 部署的项目
Worker-Deno 项目设置 是 CoddyKit 上的免费 Edge Computing with Cloudflare Workers & Deno 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 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 项目设置」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 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,全天候 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 反馈 — 无需本地设置。
此课程中的所有课时
- Cloudflare Workers 中的 Deno
- Worker-Deno 项目设置
- 共享逻辑与实用工具
- 部署与调试集成技术栈