Cloudflare Functions
Cloudflare Functions is a serverless compute platform by Cloudflare, designed for low-latency execution at the edge.
Pikku integrates with Cloudflare Functions using the Node.js runtime via the @pikku/cloudflare
package. It supports WebSockets, HTTP, and scheduled tasks.
There are multiple ways to deploy a function on Cloudflare. If you prefer an infrastructure-as-code approach, Wrangler is the recommended tool.
This guide assumes you are using Wrangler for deployment, as it provides a seamless development experience, but the core principles apply regardless of method.
Getting Started
1. Setup
To create a new Pikku project:
- npm
- Yarn
- pnpm
- Bun
npm create pikku@latest
npm create pikku@latest
# couldn't auto-convert command
npm create pikku@latest
# couldn't auto-convert command
bunx create-pikku@latest
2. Run Locally
Start a local development server:
- npm
- Yarn
- pnpm
- Bun
npm run start
yarn run start
pnpm run start
bun run start
This compiles the hello-world
example and runs a local server:
[wrangler:inf] Ready on http://localhost:8787
To verify, run:
curl http://localhost:8787/hello-world
Deploying with Wrangler
Before deployment, ensure your Cloudflare account is configured and login via:
wrangler login
Then deploy:
- npm
- Yarn
- pnpm
- Bun
npm run deploy
yarn deploy
pnpm run deploy
bun run deploy
This process will set up your project and upload the function to Cloudflare’s edge network.
If successful, the output should resemble:
Your worker has access to the following bindings:
- Vars:
- NODE_ENV: "production"
Uploaded my-app (4.63 sec)
Deployed my-app triggers (3.01 sec)
https://my-app.<your-cloudflare-zone>.workers.dev
schedule: * * * * 1
To verify the deployment, run:
curl https://my-app.<your-cloudflare-zone>.workers.dev/hello-world
How Pikku Works with Cloudflare Functions
Pikku requires two key files for Cloudflare integration:
1. Setup services
loading...
This is a wrapper around the users createSingletonServices
, but adds the LocalVariablesService to allow access to environment variables.
We want to make parts of the framework like this visible to the end user so they understand how things work. However, in the future this might move into a library or utility.
2. Entry Point
loading...
This file acts as the entry point and routes requests to the Pikku runtime.