← Cloudflare Workers / workers / testing / miniflare / core
Více Workers
Miniflare umožňuje spustit více workerů v rámci jedné instance. Všechny Workers lze definovat na stejné úrovni pomocí workers možnost.
Následuje příklad, který pomocí service vazby zvyšuje hodnotu ve sdíleném KV namespace:
import { Miniflare, Response } from "miniflare";
const message = "The count is ";
const mf = new Miniflare({
// Options shared between workers such as HTTP and persistence configuration
// should always be defined at the top level.
host: "0.0.0.0",
port: 8787,
kvPersist: true,
workers: [
{
name: "worker",
kvNamespaces: { COUNTS: "counts" },
serviceBindings: {
INCREMENTER: "incrementer",
// Service bindings can also be defined as custom functions, with access
// to anything defined outside Miniflare.
async CUSTOM(request) {
// `request` is the incoming `Request` object.
return new Response(message);
},
},
modules: true,
script: `export default {
async fetch(request, env, ctx) {
// Get the message defined outside
const response = await env.CUSTOM.fetch("http://host/");
const message = await response.text();
// Increment the count 3 times
await env.INCREMENTER.fetch("http://host/");
await env.INCREMENTER.fetch("http://host/");
await env.INCREMENTER.fetch("http://host/");
const count = await env.COUNTS.get("count");
return new Response(message + count);
}
}`,
},
{
name: "incrementer",
// Note we're using the same `COUNTS` namespace as before, but binding it
// to `NUMBERS` instead.
kvNamespaces: { NUMBERS: "counts" },
// Worker formats can be mixed-and-matched
script: `addEventListener("fetch", (event) => {
event.respondWith(handleRequest());
})
async function handleRequest() {
const count = parseInt((await NUMBERS.get("count")) ?? "0") + 1;
await NUMBERS.put("count", count.toString());
return new Response(count.toString());
}`,
},
],
});
const res = await mf.dispatchFetch("http://localhost");
console.log(await res.text()); // "The count is 3"
await mf.dispose();Směrování
Směrování můžete povolit zadáním routes prostřednictvím API,
pomocí
standardní syntaxe tras.
Upozorňujeme, že čísla portů se ignorují:
const mf = new Miniflare({
workers: [
{
scriptPath: "./api/worker.js",
routes: ["http://127.0.0.1/api*", "api.mf/*"],
},
],
});Při použití hostname, které nejsou localhost nebo 127.0.0.1, možná budete muset upravit soubor svého počítače hosts soubor, aby se tyto názvy hostitelů přeložily na
localhost. Na Linuxu a macOS se obvykle nachází v /etc/hosts. Na Windows,
se nachází v C:\Windows\System32\drivers\etc\hosts. Pro výše uvedené trasy bychom museli
připojit do souboru následující položky:
127.0.0.1 miniflare.test
127.0.0.1 api.mfAlternativně můžete přizpůsobit Host hlavičku při odesílání požadavku:
# Dispatches to the "api" worker
$ curl "http://localhost:8787/todos/update/1" -H "Host: api.mf"Při použití API určí Miniflare podle URL adresy požadavku, kterému Workeru má požadavek předat.
// Dispatches to the "api" worker
const res = await mf.dispatchFetch("http://api.mf/todos/update/1", { ... });Durable Objects
Miniflare podporuje script_name možnost pro přístup k Durable Objects
exportovaným jinými skripty. Více informací
📌 Durable Objects
pro další podrobnosti.