### Example Directory Structure for Pages Functions Source: https://edgeone.cloud.tencent.com/pages/document/162936866445025280 This example illustrates the directory structure for Pages Functions, showing how nested directories map to URL routes. The `functions` directory contains the JavaScript files that will be executed. ```directory structure functions ├── index.js ├── hello-pages.js ├── helloworld.js ├── api ├── users ├── list.js ├── geo.js ├── [id].js ├── visit ├── index.js ├── [[default]].js ``` -------------------------------- ### Pages Function Handler for GET Request with Dynamic Route Parameter Source: https://edgeone.cloud.tencent.com/pages/document/162936866445025280 This JavaScript code defines an `onRequestGet` handler for a Pages Function. It demonstrates how to access dynamic route parameters, specifically `id`, from the `context.params` object and use them in the response. This handler is triggered by GET requests to routes matching the dynamic segment. ```javascript export function onRequestGet(context) { return new Response(`User id is ${context.params.id}`); } ``` -------------------------------- ### Record Page Visits with KV Storage Source: https://edgeone.cloud.tencent.com/pages/document/162936866445025280 This example shows how to use KV storage to track the number of page visits. It retrieves the current count, increments it, and then updates the value in the KV store. Ensure the KV namespace is correctly bound in your project. ```javascript export async function onRequest({ request, params, env }) { // my_kv 是您在项目中绑定命名空间时的变量名 const visitCount = await my_kv.get('visitCount'); let visitCountInt = Number(visitCount); visitCountInt += 1; await my_kv.put('visitCount', visitCountInt.toString()); const res = JSON.stringify({ visitCount: visitCountInt, }); return new Response(res, { headers: { 'content-type': 'application/json; charset=UTF-8', 'Access-Control-Allow-Origin': '*', }, }); } ``` -------------------------------- ### Get User Geolocation Source: https://edgeone.cloud.tencent.com/pages/document/162936866445025280 This snippet demonstrates how to access and return the user's geolocation information using the `request.eo.geo` property. It's useful for tailoring content or analytics based on user location. ```javascript export function onRequest({request}) { const geo = request.eo.geo; const res = JSON.stringify({ geo: geo, }); return new Response(res, { headers: { 'content-type': 'application/json; charset=UTF-8', 'Access-Control-Allow-Origin': '*', }, }); } ``` -------------------------------- ### Connect to Supabase Database Source: https://edgeone.cloud.tencent.com/pages/document/162936866445025280 This snippet demonstrates how to connect to a Supabase database and fetch data from a 'users' table. It requires the Supabase URL and API key to be configured as environment variables (`env.supabaseUrl` and `env.supabaseKey`). ```javascript import { createClient } from '@supabase/supabase-js'; export async function onRequest({ request, params, env }) { const supabase = createClient(env.supabaseUrl, env.supabaseKey); let { data } = await supabase.from('users').select('*'); return new Response(JSON.stringify({ users: data, }), { headers: { 'content-type': 'application/json; charset=UTF-8', 'Access-Control-Allow-Origin': '*', }, }); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.