How to use cloudflare kv in node.js

269 Views Asked by At

How to use cloudflare kv in node.js

I'm already create namespace.

name = "worker"
compatibility_date = "2023-09-18"

kv_namespaces = [
  { binding = "testkv", id = "xxxxxx" }
]

And write this in src/index.js

export interface Env {
    "testkv": KVNamespace;
}

export default {
    async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {

    await env.YOUR_KV_NAMESPACE.put("KEY", "VALUE");
    const value = await env.testkv.get("KEY");
    if (value === null) {
      return new Response("Value not found", { status: 404 });
      }
    return new Response(value);
    },
};

But it not working after deployed.

I want to be able to use testkv method like put and get on my javascript code.

0

There are 0 best solutions below