Deploy a full stack application without a single server ? Code a backend api in a web browser then click deploy and after 1 second your api is up to date ? Sound fun right ? With power of Edge Computing and in this blog is The Cloudflare workers you can do crazy stuff like this
1. What the heck is Edge Computing ?
Edge computing processes data close to where it’s generated (like on your smartphone or a local server), reducing delays and improving speed. It’s like having a mini-power station nearby instead of relying on one far away. Cloud computing, in contrast, sends data to distant servers for processing. Edge computing is great for real-time needs where quick responses are essential, whereas cloud computing centralizes data processing in remote locations.
For cloudflare workers edge computings There are a lot of running v8 instance that can accept and run your js code then cloudflare will find the nearest v8 to serve user request
2. Create a worker
- There are 2 ways to create a worker
- 1. From the dashboard you can create and edit the worker’s code from the cloudflare dashboard. In Cloudflare dashboard, navigate to Worker and pages -> chose create worker then you’re ready to go.
export default {
async fetch(request, env, ctx) {
return new Response('Hello World!');
},
};
- The code of a worker look like this it work on the Fetch API of JS all request informations that call to your worker is saved in the request objects and to Response the http use the Response() built in class
- Just it the beauty of it the simple with the Fetch native api that we use every days
- 2. Use wrangler (recomandation method) wrangler is a cli tool for development application that run on cloudflare to create new worker project you can do something like this
npm create cloudflare@latest
# dev server
npm run dev
# deploy to cloudflare
npm run deploy
- Wrangler include bundle and ts compile so you can code worker in typescript and also provide you a separate development environment with real cloudflare env
- With a wrangler project you must take a look at wrangle.toml file this file for project configurations and more importants for the project bindings (which i will disscuss later)
3. Workers ecosystems
- To deploy a full stack application worker is not enough. It just a run time what we need also? Some where to save our data ? Some where to save files ? Some message queue,…
- This is when these tools come in play:
- D1: the edge computing mysql database: https://developers.cloudflare.com/d1/
- KV The edge computing key value storage: https://developers.cloudflare.com/kv/
- Queues The edge computing real time message queue: https://developers.cloudflare.com/queues/
- Durable Objects: Store objects/value across all workers: https://developers.cloudflare.com/durable-objects/
- R2 the edge computing objects storage like aws s3: https://developers.cloudflare.com/r2/get-started/
- All theses tool can access in your worker code via bindings: https://developers.cloudflare.com/workers/configuration/bindings/
- Also you can try hono a simple lightweight web server library that work perfectly with cloudflare worker runtime: https://hono.dev/
- Together these tool can help you build a full stack application without a running server locate anywhere
4. Some limitations
- Runtime limitation, worker using v8 run time which mean all your code even your imported libs must be written in plain js which can run on v8. Some nodejs library have proxy to rust or python can not work with cloudflare workers. So remember you’re working with cloudflare worker runtime not nodejs
- Pricing: yes worker is not free but the free tier is very much dont worry about this if you just building a pet projects but in real world projects you may consider the pricing
Conclusion, cloudflare worker is a wonderfull tool that you should have in your toolbox. With its power you can do a lot of things more quick and don’t need to things where can i rent a server to run my code. I have a simple template which build a RestApi with hono running on cloudflare worker here. Give it a star if it help you: https://github.com/lilhuy0405/hono-cf-worker-template. Thanks for reading and see you next time
__Coding Cat 2023__