Skip to content
当前有符合你浏览器所设置语言的版本,是否前往zh-CN版本的网站?
There is a version suitable for your browser's language settings. Would you like to go to the zh-CN language of the site?
HomeDocument

Cloudflare

H1

Cloudflare’s Workers service provides a serverless JavaScript runtime environment.

By deploying code at the edge of the user’s network, Cloudflare Workers allow requests to be executed directly at the nearest node, significantly improving access speed and response efficiency.

Getting Started

When creating an application, choose cloudflare as the runtime environment.

Terminal window
bun create milkio

Runtime

Your code will run within the Cloudflare Workers runtime, which does not use Bun or Node.js as runtimes. Therefore, when writing code in the Cloudflare Workers environment, you need to rely on the features available in vanilla JavaScript and avoid using features that depend on unsupported Node.js or Bun functionalities.

Microservices

Cloudflare Workers ultimately bundle your code into a single JavaScript file and upload it, which improves performance but also eliminates Milkio’s ability to load on demand because all of your application’s code is loaded into memory at the outset.

Therefore, to maintain high performance in Cloudflare Workers when you have a large number of APIs, you should not pack too many APIs into one Worker. You can create multiple Milkio applications and deploy them to different Workers, communicating between them using your client package.

Testing

Cloudflare Workers use Wrangler as a tool for development and deployment. However, Wrangler currently does not support the ability to directly execute a JavaScript file in its own environment. Therefore, when testing with Milkio, the tests are actually executed by Bun.

To ensure that tests accurately reflect the actual performance in the Cloudflare Workers environment, we recommend using test.client for testing. This way, the tests will send requests to Cloudflare Workers instead of executing the code you want to test in Bun.

Terminal window
handler: async (test) => {
await test.execute(...) // The API you are executing runs in Bun
await test.client.execute(...) // The API you are executing runs in Cloudflare Workers
});

Deployment

Modify your /wrangler.toml, changing the name to your Workers’ name.

#:schema node_modules/wrangler/config-schema.json
name = "your-worker-name"
main = "run-cloudflare.ts"
compatibility_date = "2023-08-23"

Run bun run deploy to deploy your application to Cloudflare Workers. If it’s your first time running this command, you may need to log in.