In this lesson, we'll install Wrangler, the Workers command-line tool. We'll then create a new Cloudflare Workers project using Wrangler, configure it, and then publish it.
Instructor: [0:00] Let's begin by installing Wrangler which is Cloudflare's command line tool for managing your workers projects. To do that we'll install the Cloudflare Wrangler package.
[0:08] We'll run MPM install-g to install it globally, and then provide the package name which is @cloudflare/wrangler. Once Wrangler is installed, we can run Wrangler to make sure that the output comes out like we would expect.
[0:23] Then we can begin creating our first project. I'll run Wrangler Generate to generate a new project and then give a project name. In this case I'll say, serverless API. It'll create a new project in the directory serverless API.
[0:39] Let's open that up in VS code and take a look at what's inside. First is our index.js file. This is the standard template that every worker's project has and it just has two things inside of it.
[0:49] First, in EventListener, which looks for the fetch event and returns the handleRequest function back as a response. The handleRequest function itself, which takes a request in and then returns a new response, that says, hello worker, with the header of content-type set to text/plain.
[1:10] Before we can deploy this project, we need to login with Wrangler to say that our Wrangler installation in our computer ties to our Cloudflare Workers account. To do this, we're on wrangler login. We'll say, yes, Wrangler can open a page in our browser.
[1:25] Once the pages open in our browser, you can see its Wrangler login and then set up my email address to manage workers with Wrangler. An API token for my email will be generated and parsed to Wrangler to grant the following permissions.
[1:38] This all looks good. I'll say, authorize Wrangler, and then you'll see it says rangler is now authorized with a new API token. You can now close this page.
[1:45] Back in our terminal, you can see that it's successfully configured and that a configuration file was added. To double-check, we can run, Wrangler who am I, which shall return output showing the account that I'm currently logged into as well as the account ID.
[1:59] Let's grab that and copy it, and then take a look at our Wrangler configuration file. Wrangler terminal is a basic config file that tells Wrangler and tells Cloudflare Workers how your project works. In order to deploy this project, we need to pass in the account ID here that we got from, Wrangler who am I. The rest of this you can leave as default.
[2:19] Once we say the file will open up to terminal again, and run, wrangler publish. It will find the project. It will skip the build because it's just JavaScript. There's no dependencies. It will successfully publish it to serverless.api.signal/nerve.workers.dev.
[2:31] You could see it returns, hello worker.
I had to be logged in Cloudflare dashboard before the Token link would work.
After the API Token authorization, the terminal stays in a "waiting for API token..." and never moves on. After a search on the web, I found that others have the same issue and the only viable solution is via [wrangler config --api-key] which is not the recommended method. I'm on a macbook (big sur).