Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/anderly/cloudflare-worker-routing
A simple Cloudflare Worker with built-in routing
https://github.com/anderly/cloudflare-worker-routing
cloudflare cloudflare-api cloudflare-worker
Last synced: 6 days ago
JSON representation
A simple Cloudflare Worker with built-in routing
- Host: GitHub
- URL: https://github.com/anderly/cloudflare-worker-routing
- Owner: anderly
- License: mit
- Created: 2018-08-13T22:59:52.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2018-08-30T14:16:06.000Z (about 6 years ago)
- Last Synced: 2024-08-01T16:34:38.400Z (3 months ago)
- Topics: cloudflare, cloudflare-api, cloudflare-worker
- Language: JavaScript
- Homepage:
- Size: 103 KB
- Stars: 40
- Watchers: 5
- Forks: 10
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-cloudflare - Worker with built-in Router - Allows you to separate your worker logic into different functions and/or controllers. (Workers / Recipes)
- awesome-cloudflare - Worker with built-in Router - Allows you to separate your worker logic into different functions and/or controllers. (Workers / Recipes)
README
# A simple Cloudflare Worker with built-in routing
Here's a simple CloudFlare Worker with a built-in router that allows you to separate your worker logic into different functions and/or "controllers" so you can achieve a clean separation of concerns while working within the 1 script limit for non-Enterprise plans.
Sample route definitions:
router.get('/cloudflare', SampleController.index);
router.post('/cloudflare', SampleController.store);
router.get('/cloudflare/:id', SampleController.show);
router.put('/cloudflare/:id', SampleController.update);
router.delete('/cloudflare/:id', SampleController.destroy);
router.get('/cloudflare/routes/:id', (req) => {
return response('Response from closure instead of controller: id=' + req.params.id);
});- [src/index.js](src/index.js) is your main CloudFlare Worker entrypoint. Update the routes in the file and point to your functions / controller.
- [src/sample-controller.js](src/sample-controller.js) contains an example of a basic controller
- [src/router.js](src/router.js) contains the basic router (feedback welcome)
- [src/helpers.js](src/helpers.js) contains some simple response/redirect helper functions
- [src/http-method.js](src/http-method.js) is just a simple enum for HTTP verbs.Credits to [Dave Willenberg](https://github.com/detroitenglish) and his [Password pwnage CloudFlare Worker](https://github.com/detroitenglish/pw-pwnage-cfworker) for the auto-deploy script and webpack config.
---
## Quick Start
1. Rename `example.cloudflare.env` to `cloudflare.env` and edit the values as needed.
2. Update the **index.js** file with your routes, use function closures or separate controller files and go to town with a simple CloudFlare Worker with built-in routing!
3. Install deps with `npm install`
4. Launch 🚀 with `npm run deploy`## Try it live
Try out the following routes from the sample repo live here:
- GET https://anderly.com/cloudflare/
- GET https://anderly.com/cloudflare/12345
- GET https://anderly.com/cloudflare/routes/12345### License
MIT