workers.dev: Praxis makes perfect

Today, I decided to learn how to use Cloudflare Workers.

As a tiny project, I decided to make something to render a single line of markdown, chosen at random from a file. The file we will use for testing is us_atrocities.md from dessalines' 'essays' repo on GitHub.

Here's the vague idea:

  1. Pull the file from GitHub
  2. Choose a random line that starts with - (a list entry)
  3. Strip the leading -
  4. Render out the markdown to an HTML page with a nice CSS sheet
  5. Show this page to the user

Getting Started

To start, I had to install cloudflare-wrangler from the AUR. Everything went smoothly, and I'm ready to develop my first worker.

I follow the instructions:

$ wrangler generate 'my-first-cloudflare-worker'
$ vim index.js

I write some code as if I'm working in node.js, wanting to use the 'marked' library for markdown rendering, but it doesn't work! require isn't defined.

The fix is, (after ten minutes' confusion) to set type = "webpack" in wrangler.toml.

Everything else is a breeze, I can almost see how 'serverless' came to be so hyped.

One wrangler deploy and the site is live. Fantastic.