DEV Community

Cover image for Introducing Cloud Rover. Turn Cloudflare Workers into Fully Functional Backends.
Shazin
Shazin

Posted on • Originally published at shazin.me

Introducing Cloud Rover. Turn Cloudflare Workers into Fully Functional Backends.

Last year I was looking for serverless solutions for building apps without having to manage servers by myself. I knew about cloudflare worker befor. But never thought it is built for running we services or building api on it. However upon using it I totally understood how it works. And thought all it needs is a routing system, proper request handling and some form of rules. That's all.

For those of you who don't know what cloudflare worker is here's a short note from cloudflare docs

A serverless platform for building, deploying, and scaling apps across Cloudflare's global network ↗ with a single command — no infrastructure to manage, no complex configuration

I started working on a lightweight middleware library to simplify things on worker. Initially I created it for my personal usage, didn't have any intention for sharing in public. However, I don't know if any new library exists now, but I just feel like I should share this, if anyone finds it helpful that will be great for me. So basically it just adds a lightweight layer on top of the worker and provides the followings

  • Easy routing
  • Seamless Request Handling: Process HTTP methods like GET, POST, and DELETE with built-in utilities.
  • Structured Response Management: Respond with JSON, text, or HTML without repetitive code.
  • Easy response handling (simplified from the original Response Interface )
  • Error handling
  • And other miscellaneous things like CORS handling, different kind of routes like schematic routes, standard handler function etc.

Most importantly it provides a standard structure which as of writing now by default cloudflare worker doesn't provide. When you create a new worker you’ll see something like this

export default {
  async fetch(request, env, ctx) {
    return new Response("Hello World!");
  },
};
Enter fullscreen mode Exit fullscreen mode

And that's it from here you go full DIY mode. I created cloud-rover to simplify this process.

Using Rover

Rover is a very simple thing. You create a cloudflare worker normally then you install the npm package cloud-rover

Step-1

So start by creating a new worker and installing cloud-rover

# create worker
npm create cloudflare@latest -- my-first-worker
cd my-first-worker
# install cloud-rover
npm install cloud-rover
Enter fullscreen mode Exit fullscreen mode

That's all you need as set-up.

Details Here

Step 2

Now go to the default index.js/ts file where you’ll see something similar I shared earlier.

Now you need to define

  1. A router using createRouter method
  2. Define a handler
  3. Pass Rover as a response.

So instead of the default code provided by cloudflare we’ll use something like this

import { createRouter, RC, Rover, reply } from "cloud-rover";
// Define  a router
const router = Router([
  {
    path: "/",
    handler: index_handler,
  },
]);

// the index handler function
async function index_handler(rc: RC): Promise<Response> {
  return reply.text("Hello World From Rover!");
}

export default {
  async fetch(request, env, ctx): Promise<Response> {
    return Rover(request, router);
  },
} satisfies ExportedHandler<Env>;
Enter fullscreen mode Exit fullscreen mode

That's all, you are now using cloud-rover. Just add more routes and functionalities.

Consideration

It might lack features and so does all softwares. I just added the essentials, if you think you can make it better feel free to contribute. If you need any feature you can create a feature request too. I made this library for my own usage and it might face bugs, feel free to create issues on github.

Open Source and Contributions

Like all meaningful tools, Rover is open source. It’s available on GitHub, and I’m excited to see how the developer community will use it to build amazing projects. Contributions, suggestions, and feedback are all welcome!

What’s Next?

Check out the official documentation for detailed guides, API references, and more examples. Or head straight to GitHub to explore the source code and get started.

Join the Journey

If Cloud Rover excites you as much as it excites me, consider starring the project on GitHub, sharing it with your network, or contributing code. Together, we can make Cloudflare Workers even more powerful.

Let’s redefine serverless backend development.
Thank you for reading.

Top comments (0)