|
| 1 | +--- |
| 2 | +updated: YYYY-MM-DD |
| 3 | +difficulty: Beginner | Intermediate | Expert |
| 4 | +content_type: 📝 Tutorial |
| 5 | +pcx_content_type: tutorial |
| 6 | +title: Tutorial title. Second-person imperative verb phrase that reflects user goal or job-to-be-done. For example, 'Create a Worker' or 'Build a Pages application'. |
| 7 | +products: |
| 8 | + - Workers |
| 9 | +languages: |
| 10 | + - TypeScript |
| 11 | +--- |
| 12 | + |
| 13 | +import { Render, PackageManagers } from "~/components"; |
| 14 | + |
| 15 | +(Introduce the purpose of this tutorial here. Describe the application which will be built by the end of this tutorial and the tools that will be used to achieve this. You can also optionally describe the intended audience, include a [GitHub link](https://github.com/) to completed code, and even outline a summary of the steps that the reader will be performing throughout this tutorial. |
| 16 | + |
| 17 | +In this tutorial, you will learn how to run serverless script on the web by creating a Worker using Cloudflare's CLI tools C3 and Wrangler. |
| 18 | + |
| 19 | +## Prerequisites |
| 20 | + |
| 21 | +(List out any required prerequisites before the reader can begin following this tutorial. If your tutorial requires Workers, you can load pre-written Workers prerequisites by including the render below)) |
| 22 | + |
| 23 | +<Render file="prereqs" product="workers" /> |
| 24 | + |
| 25 | +(Additional prerequisites will need to be manually added like below) |
| 26 | + |
| 27 | +- Prerequisite 3 |
| 28 | +- Prerequisite 4 |
| 29 | +- Prerequisite 5 |
| 30 | + |
| 31 | +## 1. Create a Worker. (The step title should describe what is being achieved within the step. If a step is becoming too large, see if it can be broken down into smaller steps.) |
| 32 | + |
| 33 | +(To include a CLI command with tabs to select between npm, yarn or pnpm, use the example below.) |
| 34 | + |
| 35 | +First, use the `c3` CLI to create a new Cloudflare Workers project. |
| 36 | + |
| 37 | +<PackageManagers |
| 38 | + type="create" |
| 39 | + pkg="cloudflare@latest" |
| 40 | + args={"<PROJECT_NAME>"} |
| 41 | +/> |
| 42 | + |
| 43 | +Replace `<PROJECT_NAME>` with your desired project name. |
| 44 | + |
| 45 | +## 2. Tets run your Worker on a local server |
| 46 | + |
| 47 | +(To include a shell command, use the examples below [For code block guidelines read our style guide](/style-guide/formatting/code-block-guidelines/).) |
| 48 | + |
| 49 | +Next, change into the newly created Worker's directory. |
| 50 | + |
| 51 | +```sh title="Change directory into worker" |
| 52 | +cd <PROJECT_NAME> |
| 53 | +``` |
| 54 | +Now we can run our Worker locally to test that it works. |
| 55 | + |
| 56 | +```sh title="Run Worker on a local development server" |
| 57 | +npx wrangler dev |
| 58 | +``` |
| 59 | + |
| 60 | +(To include a number list such as for following numbered steps, use the example below.) |
| 61 | + |
| 62 | +1. Step 1 |
| 63 | +2. Step 2 |
| 64 | + |
| 65 | +## 3. (Additional tutorial tips) |
| 66 | + |
| 67 | +(JavaScript example.) |
| 68 | + |
| 69 | +```js |
| 70 | +--- |
| 71 | +filename: src/index.js |
| 72 | +--- |
| 73 | +export default { |
| 74 | + async fetch(request, env, ctx) { |
| 75 | + return new Response("Hello World!"); |
| 76 | + }, |
| 77 | +}; |
| 78 | +``` |
| 79 | + |
| 80 | +(Wrangler toml file example.) |
| 81 | + |
| 82 | +```toml title="wrangler.toml" |
| 83 | +#:schema node_modules/wrangler/config-schema.json |
| 84 | +name = "<PROJECT_NAME>" |
| 85 | +main = "src/index.ts" |
| 86 | +compatibility_date = "YYYY-MM-DD" |
| 87 | +compatibility_flags = ["nodejs_compat"] |
| 88 | + |
| 89 | + [ai] |
| 90 | + binding = "AI" |
| 91 | +``` |
| 92 | + |
| 93 | +(Aside examples. For more details read our [style guide documentation on Notes/tips/warnings](/style-guide/documentation-content-strategy/component-attributes/notes-tips-warnings/#recommendations).) |
| 94 | + |
| 95 | +:::note[Aside example] |
| 96 | +An aside is a colored info box or aside with content (text, images, lists, code blocks) that adds relevant notes that do not fit the main text |
| 97 | +::: |
| 98 | + |
| 99 | +:::caution[Aside example] |
| 100 | +A caution warns users of specific behavior that can break functionality or impact security. |
| 101 | +::: |
| 102 | + |
| 103 | +(At the end of the tutorial's steps, summarize what has been successfully achieved by the reader through completing this tutorial) |
| 104 | + |
| 105 | +You have successfully created, tested and deployed a Worker. |
| 106 | + |
| 107 | +## Related resources |
| 108 | + |
| 109 | +(Cloudflare docs link example) |
| 110 | + |
| 111 | +To build more with Workers, refer to [Tutorials](/workers/tutorials/). |
| 112 | + |
| 113 | +(External link example) |
| 114 | + |
| 115 | +If you have any questions, need assistance, or would like to share your project, join the Cloudflare Developer community on [Discord](https://discord.cloudflare.com) to connect with other developers and the Cloudflare team. |
0 commit comments