Upgrade to Pro — share decks privately, control downloads, hide ads and more …

すぐ使える Cloudflare Workers!

Kei Kamikawa
March 09, 2024
3.2k

すぐ使える Cloudflare Workers!

Kei Kamikawa

March 09, 2024
Tweet

Transcript

  1. ΞϓϦέʔγϣϯͷ։ൃ؀ڥΛ੔͑Δ $ tree my-app --gitignore my-app ├── README.md ├── package.json

    ├── src │ └── index.ts ├── tsconfig.json 
 └── wrangler.toml
  2. import { Hono } from "hono"; const app = new

    Hono<{ Bindings: { ENV: string; }; }>(); app.get("/", (c) => c.text(c.env.ENV)); export default app; ؀ڥม਺Λ੔͑Δ JOEFYUT
  3. ؀ڥม਺Λ੔͑Δ $ cd my-app && npm run dev 
 $

    curl http://localhost:8787 
 local
  4. ϛυϧ΢ΣΞΛ੔͑Δʢྫ,7ʣ $ npx wrangler kv:namespace create TEST_KV ⛅ wrangler 3.17.1

    ------------------- 🌀 Creating namespace with title "my-app-TEST_KV" ✨ Success! Add the following to your configuration file in 
 your kv_namespaces array: { binding = "TEST_KV", id = "da56a0e3ee6641fabf26b726e60fa550" }
  5. const app = new Hono<{ Bindings: { TEST_KV: KVNamespace }

    }>(); app.get("/counter", async (c) => { const key = c.req.query()["key"] || "defaultKey"; const count = Number(await c.env.TEST_KV.get(key)); const count = !isNaN(value) ? value + 1 : 1; await c.env.TEST_KV.put(key, String(count)); return c.text(String(count)); }); export default app; JOEFYUT ϛυϧ΢ΣΞΛ੔͑Δʢྫ,7ʣ