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:
- Pull the file from GitHub
- Choose a random line that starts with
-(a list entry)
- Strip the leading
- Render out the markdown to an HTML page with a nice CSS sheet
- Show this page to the user
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
Everything else is a breeze, I can almost see how 'serverless' came to be so hyped.
wrangler deploy and the site is live. Fantastic.