Loading routes from Cloudflare KV?

I need to store routes in a dynamic fashion (be it env or KV) since routes will controlled from a third party -- is there a way to load these without adding a "default" catchall route that then has access to env/kv context from where the routes can be loaded? Optimally I can avoid a route.all(...) that has the logic to fetch this and then add additional routes.
3 Replies
ambergristle
ambergristle2mo ago
i haven't worked w cloudflare much, but this seems like the base case, right?
export default {
/**
* @param {Request} request
* @param {Env} env
* @param {ExecutionContext} ctx
* @returns {Response}
*/
fetch(request, env, ctx) {
console.log("Hello Cloudflare Workers!");

return new Response({ welcome: 'hi' }, {
headers: {
"content-type": "application/json",
},
});
},
};
export default {
/**
* @param {Request} request
* @param {Env} env
* @param {ExecutionContext} ctx
* @returns {Response}
*/
fetch(request, env, ctx) {
console.log("Hello Cloudflare Workers!");

return new Response({ welcome: 'hi' }, {
headers: {
"content-type": "application/json",
},
});
},
};
seems simple enough to grab the route/function name from KV and invoke
jbergstroem
jbergstroemOP2mo ago
right; was just thinking if there was a way to not load routes inside the "/" entrypoint but I guess there isn't.
ambergristle
ambergristle2mo ago
sorry, i'm not sure i understand what you mean you want an app with no entry point at all?

Did you find this page helpful?