Skip to main content
Version: 1.x.x

Using Rover

Once you have installed rover middleware you can begin to use it. Earlier we stated the project structure in Getting started guide as follows

my-first-worker
├── package.json
├── pnpm-lock.yaml
├── src
│   └── index.ts
├── test
│   ├── index.spec.ts
│   └── tsconfig.json
├── tsconfig.json
├── vitest.config.mts
├── worker-configuration.d.ts
└── wrangler.toml

Here we are interested in src/index.ts file as it the default entrypoint in a worker project.

tip

You can change the default entrypoint by editing wrangler.tml. by updating the lin main = "src/index.ts" to point to your desired file.

Create a Router​

Start by creating a router

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

// the index handler function
async function indexHandler(rc) {
return reply.text("Hello World From Rover!");
}

Here, indexHandler is the handler function for that router. More on router will be discussed on Router section

Call the Middleware​

By default your entry file or index.js will look like this

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

Edit this file src/index.js:

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

Notice here the router is the router we created earlier

So now our index.js file will look like this

import { reply, createRouter, Rover } from "cloud-rover"
const router = createRouter(
[
{
path: "/",
handler: indexHandler,
method: "POST"
}
]
)

async function index_handler(rc) {
return reply.text("Hello World from Rover!")
}

export default {
async fetch(request, env, ctx) {
return Rover(request, router, env, ctx)
},
} satisfies ExportedHandler<Env>;

Run The Project​

Follow cloudflare's standard run command for running the project

npm run dev

It should start the development server

> wrangler dev


⛅️ wrangler <version code>
-------------------

⎔ Starting local server...
[wrangler:inf] Ready on http://localhost:8787

And you should see a "Hello World from Rover!" at http://localhost:8787