Write less, do more at the edge: Introducing expressly
Don't you ever wish Compute@Edge worked like the framework you already know? We just launched expressly, a lightweight and minimalist routing layer for JavaScript apps running on Fastly's Compute@Edge, and inspired by the popular Node.js framework, Express.
Because Compute@Edge is low level, developers can iterate on top of it with libraries like this very quickly, in their preferred language. My colleague Kailan did something similar recently with a library for Edge Side Includes in Rust.
expressly lets you focus on your business logic, and makes it easier than ever to get started with Compute@Edge in JavaScript. It’s packed with lots of useful features:
Simplify complex routing logic using Express-style path strings;
Define path parameters;
Manage cookies;
Work with search parameters;
Control the flow of a request using middleware and error-handling middleware functions;
More easily manipulate Vary and Surrogate-Key headers;
And so much more!
Write less, do more
My colleagues will attest that little brings me more joy than deleting lots of code. As I was rewriting some code examples on Fastly’s Developer Hub using expressly, I found myself increasingly more joyful.
To give you a taste of what it’s like to write Compute@Edge apps with expressly, I’ve contrived an application scenario with the following requirements:
On requests to
/base64/[something]
:read a segment of the URL path;
write it into a cookie;
decode it and put it in the response body;
return a 500 status if decoding fails, and include the error message in the response body;
Forward all requests to
/status/[anything]
to an origin;Send either
405
(Method Not Allowed) or404
(Not Found) responses in any other case, as appropriate.
Let’s take a look at how you might implement this without expressly:
…and another look at the same application logic, this time using expressly:
Isn’t that delightful? Expressly’s route matcher, built-in path parameter parser, and default error-handling middleware make light work of complex scenarios like the one I imagined. You simply must try it for yourself!
Getting started with expressly
If you don't have Node.js and a JavaScript Compute@Edge service set up yet, start by getting set up.
Install expressly
Install expressly from the npm registry:
npm i @fastly/expressly
Your first expressly app
Replace the contents of your Compute@Edge app's src/index.js
with the following:
import { Router } from "@fastly/expressly";
const router = new Router();
router.get("/", async (req, res) => { return res.send("Hello world!");});
router.listen();
Try it out
Start your app locally:
fastly compute serve
This will start your service on http://localhost:7676.
Ready?
@fastly/expressly@1.0.0-alpha.x
is currently available for developer testing. Check out our growing collection of code examples using expressly on Fastly’s Developer Hub for more inspiration.
If you want to try this for yourself, clone the second fiddle in this blog post and test it out with your own origin without creating a Fastly account. When you’re ready to publish a full global service, you can sign up for a free trial of Compute@Edge and get started with expressly right away.