Serverless with Cloudflare
What is serverless computing?
On demand server for computing. It is an abstraction relevant to application development and not consumption. An application is a software service that executes whenever a user needs it. Application could wait for a user request and keep on standby (server) or it could leave the instruction with another application (serverless platform) to make it available whenever a user request comes. Serverless computing is clearly not the answer to every application need but it makes sense for a lot of use cases.
Cloudflare Workers
A very basic example using cloudflare worker. The worker code adds an event handler for fetch and returns a string ‘hello world!’ for all requests.
addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
return new Response("Hello world");
}
Cloudflare Workers is a serverless application platform. The examples above are the application and the instruction for on demand execution is handled by the cloudflare platform.
https://my-worker.makeall.workers.dev/
Wrangler Setup
Cloudflare provides an application development tool to develop and test serverless applications.
npm install -g @cloudflare/wrangler
wrangler --version
wrangler login
# Open Browser
# Login and get API Token
# Config file at
/home/ranu/.wrangler/config/default.toml
New Project
wrangler generate my-worker
#edit wrangler.toml
# dev test
wrangler dev
# publish live
wrangler publish