All Products
Search
Document Center

Edge Security Acceleration:Use Edge Routine to view key values in KV

Last Updated:May 23, 2025

Edge Routine allow you to host lightweight static websites on ESA edge points of presence (POPs), providing lower latency responses for your business and reducing the load on the origin server. This topic describes how to use Edge Routine to view key values in Edge KV.

Sample code

  • Expected result: Use Edge Routine to view key values in Edge KV.

  • Language: JavaScript

  • Sample code:

    // Defines an asynchronous function named handleRequest
    async function handleRequest(request) {
      try {
      // Initializes the connection of Edge KV
        const edgeKV = new EdgeKV({ namespace: "kv" });// The namespace is kv
        let getType = { type: "text" };
        let value = await edgeKV.get("key", getType);
        // Checks whether the key exists
        if (value === undefined) {
          return "EdgeKV get: key not found";// Error: A string instead of Response is returned
        } else {
          return new Response(value);// Correct: Response is returned
        }
      } catch (e) {
        return "EdgeKV get error" + e;// Error: A string instead of Response is returned
      }
    }
    // Exports the default fetch processing function
    export default {
      async fetch(request) {
        return handleRequest(request);// Returns the result of handleRequest
      }
    };

Deployment effect

You can view the specific key value by accessing the function associated with Edge Routine or the specified routing address in the browser.

Assume that the key value is 888888 and the routing address is https://10.10.10.10. When you access https://10.10.10.10 in the browser, 888888 is displayed.

image