Adding Simple Routing to Cloudflare Workers

I’ve been "playing" with serverless for years now, but honestly still feel new to it. When it comes to organization in a project that uses serverless functions, I’ve typically tried to build one function per operation. So for example, if I had a need to get a list of cats, I’d have one function. If I had a need to get information about a cat based on an identifier, I’d probably build a second one. That being said, I recently came across an example Cloudflare function that did something cool – it used a router, specifically the very lightweight itty-router. Let me share an example of how it works. What is a Router? # So I kinda assume most folks know what... more →
Posted in: JavaScript

Building a Generic RSS Parser Service with Cloudflare Workers

About once every three months I’ll write a quick JavaScript demo and attempt to fetch someone’s RSS feed… and then remember that the vast majority of RSS feeds don’t specify a CORS header to allow remote scripts to load them. I know this – and yet I still tend to forget. I thought it would be kind of fun to build a serverless API via Cloudflare Workers to handle loading, parsing, and returning a RSS feed with CORS allowed. I figured this would be pretty easy, but I ran into a snag right away. Workers and NPM Modules # Cloudflare Workers is Node.js compatible… with some issues. Cloudflare has a documentation page on it addressing what you may run into, and for... more →
Posted in: JavaScript

Connecting Cloudflare Workers with Service Bindings

I’ll warn you ahead of time and say this post isn’t too much more than what you can find in the documentation, but I wanted to see it work for myself so I had to setup a test locally. Cloudflare Service bindings are a way for one Worker to connect to another. That seems simple enough, but while it defines a "connection", that connection is completely internal to the Cloudflare environment. I.e., incredibly fast with much lower latency. Let’s consider a simple example. The Receiver # I began by creating a worker, named backworker, with just a simple message: export default { async fetch(request, env, ctx) { return new Response('Hello from Backworker'); },}; The Front... more →
Posted in: JavaScript

Adding Caching to a Cloudflare Worker

Last week I blogged about my first experience building a Cloudflare Worker serverless function. In that post, I built a simple serverless function that wrapped calls to the Pirate Weather API, a free and simple-to-use API for getting weather information. For today’s post, I thought I’d show how easy it is to add a bit of caching to the worker to help improve its performance. As with my last post, I’ve also got a video walkthrough of everything you watch instead. (Or read and watch, go crazy!) The Application # In the last post, I shared the complete code of the Worker, but let me share it again: // Lafayette, LAconst LAT = 30.22;const LNG = -92.02;export default { async fetch(request,... more →
Posted in: JavaScript