cloudflare / worker-template-router

231 stars 84 forks source link

Switch to using itty-router in the worker template #35

Closed jb3 closed 3 years ago

jb3 commented 3 years ago

This PR switches the template away from using a custom made worker (currently defined in router.js) and instead uses the itty-router router to reduce complexities with routing. It removes router.js and adds itty-router to the dependencies defined in package.json.

There are a number of routes available to demonstrate different routing techniques, such as path fragments, fetching headers and more.

There is an example of this template currently running at https://router.jb3.workers.dev/, feel free to play with it!

GET /

An index page, very simple.

GET /example/:text

Returns a base64 encoding of provided text, useful for demonstrating path fragments and taking user input from URLs.

POST /post

With no body, returns the ASN and colo of the incoming request.

When provided with a JSON body it will additionally return that data as well.

Fallback

There is a fallback provided to show how to implement a custom 404 page and avoid raising worker exceptions.

kristianfreeman commented 3 years ago

This is so awesome!! I would love to get this merged but I want to share it with the Workers team first and give them a heads-up that this is planned.

cc @kwhitley as this is relevant to him too :)

evanderkoogh commented 3 years ago

Fine @signalnerve.. I'll throw away at least the hour I spend on fixing up the router one and porting it to Typescript 🗡️ We should make the change over at the Typescript template as well then..

kwhitley commented 3 years ago

Awww, I feel the love, fellas - thanks!! Just a heads up, I'm planning on adding a few super-lightweight utility functions (e.g. asJSON(content: object): Response) and middleware that might help abbreviate some of the boilerplate even further (if you like). I'll be gathering up the most-essential of these little building blocks and releasing them in a tree-shakable path on itty-router/extras or something.

Also quick side note, I def recommend the pattern that @signalnerve used on the Cloudflare Pages tutorial with a final:

router.all('*', () => new Response("Not found", { status: 404 }))

Especially with mixed-method routing (e.g. GET + POST), you want to catch any of those missing routes rather than failing to respond (result: Cloudflare Error page)!

Let me know if you guys catch any issues with the router or can see any room for improvement/clarification on the README. A full docs site with much more elaborate examples will follow (e.g. making feature-rich routers around itty), and I'll be sure to point back to all these docs.

Also... if you guys can, you know... get me approved for the Durable Objects beta, I'm dying to start writing tooling around those! 😉 🙏

jb3 commented 3 years ago

Hey Kevin, nice to meet you, great work on itty-router! 😄

Love the idea of the the utility functions, I'll be happy to add those here once introduced.

There is already a fallback router.all on line 74 with that syntax 👍. See https://router.jb3.workers.dev/notreal.

From someone who hasn't used routing before with Workers the router docs were great, very simple to pick up.

Also looking forward to Durable Objects here 😄

kwhitley commented 3 years ago

🥳

jb3 commented 3 years ago

:shipit::shipit::shipit: