跳转到主要内容

First Worker

import { Render, PackageManagers, DashButton } from "~/components";

Build and deploy your first Worker

You can deploy your first Worker via the Cloudflare dashboard or programmatically using your terminal.

You must have a Cloudflare account to create a Worker. To get started with Cloudflare, refer to Create account.

Via the Cloudflare dashboard

Via C3 and Wrangler

Prerequisites

Create and deploy your first Worker

To create your Worker project, run:

<PackageManagers type="create" pkg="cloudflare@latest" args={"first-worker"} />

This will prompt you to install the create-cloudflare package, and lead you through setup.

<Render file="c3-post-run-steps-hello-world" product="workers" params={{ type: "Worker only", lang: "JavaScript", }} />

You will be asked if you would like to deploy the project to Cloudflare.

  • If you choose to deploy, you will be asked to authenticate (if not logged in already), and your project will be deployed to the Cloudflare global network and available on your custom workers.dev subdomain.

  • If you choose not to deploy, go to the newly created project directory to begin writing code. Deploy your project by running the wrangler deploy command.

Refer to How to run Wrangler commands to learn how to run Wrangler commands according to your package manager.

In your Worker project directory, C3 has generated the following:

  1. wrangler.jsonc: Your Wrangler configuration file.
  2. index.js (in /src): A minimal 'Hello World!' Worker written in ES module syntax.
  3. package.json: A minimal Node dependencies configuration file.
  4. package-lock.json: Refer to npm documentation on package-lock.json.
  5. node_modules: Refer to npm documentation node_modules.

To continue building your Worker, open the index.js file to write your code. Refer to Examples to use ready-made code you can experiment with.

Summary

You have learned how to:

  • Create and deploy a Worker project using the Cloudflare dashboard and programmatically, using your terminal.

In the next section, you can follow a video tutorial to create your first Cloudflare Workers application.

  • Get started guide - Create a new Worker with Cloudflare Workers' Get started guide.