Code Monkey home page Code Monkey logo

cloudflare-worker-routing's Introduction

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);
});

Credits to Dave Willenberg and his Password pwnage CloudFlare Worker 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:

License

MIT

cloudflare-worker-routing's People

Contributors

anderly avatar kentonv avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

cloudflare-worker-routing's Issues

Exceeded CPU errors

First off, this is a great library. I've implemented it and its been easy to use. Thanks for sharing.

Unfortunately, I'm pretty confident at this point that this takes too much time to execute and will eventually lead to a Exceeded CPU error. The problem is that Cloudflare doesn't start throwing that error until you hit serious traffic in a short period of time (somewhere in the range of 1-5k hits within a 5 minute period is what I've gathered).

Once i removed the router I've eliminated that error. It was a pain to troubleshoot because Cloudflare doesn't provide any insight into what is going on on their end.

It may also depend on how many routes you have configured. I have 8 routes set up in my scenario.

Just want to make any others aware and save them countless hours of troubleshooting I spent on this :)

Babel version & config and other updates

Thanks for the shout-out ๐Ÿ˜Ž

Wanted to give you quick heads-up:

  • Babel 7 is still technically in beta... I use it in prod, but I'm crazy like that
  • babel@polyfill and babel@preset-env are not applied in the build-step. You'll need to pass your entry script through babel@polyfill, and use modules rules with babel-loader to do so
  • scripts/deploy.js error handling messages are ๐Ÿ’ฉ

I just pushed an update to my own cf-worker with fixes. Also changed the browserslist target to Chrome >= 57.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.