{"id":89208,"date":"2026-08-02T16:24:47","date_gmt":"2026-08-02T08:24:47","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/89208.html"},"modified":"2026-08-02T16:24:47","modified_gmt":"2026-08-02T08:24:47","slug":"%e4%bb%8e%e9%9b%b6%e9%83%a8%e7%bd%b2%e5%85%a8%e6%a0%88%e7%bd%91%e7%ab%99%ef%bc%9acloudflare-pages-functions-d1-%e5%ae%8c%e6%95%b4%e5%ae%9e%e8%b7%b5","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/89208.html","title":{"rendered":"\u4ece\u96f6\u90e8\u7f72\u5168\u6808\u7f51\u7ad9\uff1aCloudflare Pages + Functions + D1 \u5b8c\u6574\u5b9e\u8df5"},"content":{"rendered":"<p>Cloudflare \u7684\u8fd9\u5957\u7ec4\u5408\u9002\u5408\u4e2d\u5c0f\u578b\u5168\u6808\u9879\u76ee&#xff1a;<\/p>\n<ul>\n<li>Cloudflare Pages&#xff1a;\u6258\u7ba1 React\u3001Vue\u3001Vite\u3001\u9759\u6001\u7ad9\u70b9\u7b49\u524d\u7aef\u4ea7\u7269&#xff1b;<\/li>\n<li>Cloudflare Pages Functions&#xff1a;\u5728\u8fb9\u7f18\u8fd0\u884c API \u4ee3\u7801&#xff0c;\u4e0d\u9700\u8981\u5355\u72ec\u8d2d\u4e70\u6216\u7ef4\u62a4\u4f20\u7edf\u670d\u52a1\u5668&#xff1b;<\/li>\n<li>Cloudflare D1&#xff1a;Cloudflare \u6258\u7ba1\u7684 serverless SQLite \u6570\u636e\u5e93&#xff1b;<\/li>\n<li>GitHub \u96c6\u6210&#xff1a;\u63a8\u9001\u4ee3\u7801\u540e\u81ea\u52a8\u6784\u5efa\u3001\u90e8\u7f72&#xff1b;PR \u6216\u975e\u751f\u4ea7\u5206\u652f\u53ef\u4ee5\u5f97\u5230\u9884\u89c8\u73af\u5883\u3002<\/li>\n<\/ul>\n<p>Pages Functions \u53ef\u4ee5\u901a\u8fc7\u7ed1\u5b9a\u76f4\u63a5\u8bbf\u95ee D1&#xff1b;\u524d\u7aef\u9759\u6001\u8d44\u6e90\u4e0e API \u53ef\u4ee5\u540c\u57df\u90e8\u7f72&#xff0c;\u56e0\u6b64\u901a\u5e38\u4e0d\u9700\u8981\u989d\u5916\u5904\u7406\u8de8\u57df\u95ee\u9898\u3002(developers.cloudflare.com)<\/p>\n<hr \/>\n<h3>\u4e00\u3001\u6700\u7ec8\u67b6\u6784<\/h3>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260802082446-6a6efece29206.png\" \/><\/p>\n<p>\u4e00\u6b21\u8bf7\u6c42\u7684\u5927\u81f4\u8def\u5f84\u5982\u4e0b&#xff1a;<\/p>\n<li>\u6d4f\u89c8\u5668\u4ece Pages \u83b7\u53d6 React\/Vite \u6784\u5efa\u540e\u7684\u9759\u6001\u6587\u4ef6&#xff1b;<\/li>\n<li>\u524d\u7aef\u901a\u8fc7 fetch(&#034;\/api\/&#8230;&#034;) \u8c03\u7528\u540c\u57df API&#xff1b;<\/li>\n<li>functions\/ \u76ee\u5f55\u4e2d\u7684 Pages Functions \u5904\u7406\u8bf7\u6c42&#xff1b;<\/li>\n<li>Function \u4f7f\u7528 context.env.DB \u8bbf\u95ee D1&#xff1b;<\/li>\n<li>GitHub \u7684 main \u5206\u652f\u6709\u65b0\u63d0\u4ea4\u65f6&#xff0c;Pages \u81ea\u52a8\u6784\u5efa\u5e76\u66f4\u65b0\u751f\u4ea7\u7ad9\u70b9\u3002<\/li>\n<p>Cloudflare Pages \u7684 Git \u96c6\u6210\u652f\u6301\u63a8\u9001\u81ea\u52a8\u90e8\u7f72\u3001PR\/\u5206\u652f\u9884\u89c8 URL&#xff0c;\u4ee5\u53ca GitHub \u5185\u7684\u6784\u5efa\u72b6\u6001\u68c0\u67e5\u3002(developers.cloudflare.com)<\/p>\n<hr \/>\n<h3>\u4e8c\u3001\u51c6\u5907\u6761\u4ef6<\/h3>\n<p>\u5f00\u59cb\u524d\u9700\u8981\u51c6\u5907&#xff1a;<\/p>\n<ul>\n<li>\u4e00\u4e2a Cloudflare \u8d26\u53f7&#xff1b;<\/li>\n<li>\u4e00\u4e2a GitHub \u4ed3\u5e93&#xff1b;<\/li>\n<li>Node.js 20 \u6216\u66f4\u9ad8\u7248\u672c&#xff1b;<\/li>\n<li>npm\u3001pnpm \u6216 yarn&#xff1b;<\/li>\n<li>\u4e00\u4e2a\u672c\u5730 Git \u73af\u5883&#xff1b;<\/li>\n<li>\u53ef\u9009&#xff1a;Cloudflare \u81ea\u5df1\u6258\u7ba1\u7684\u57df\u540d\u3002<\/li>\n<\/ul>\n<p>\u672c\u6587\u4ee5 React &#043; Vite &#043; TypeScript \u4e3a\u4f8b&#xff0c;\u4f46\u540e\u7aef\u90e8\u5206\u540c\u6837\u9002\u7528\u4e8e Vue\u3001Svelte\u3001Astro \u6216\u7eaf\u9759\u6001 HTML \u9879\u76ee\u3002<\/p>\n<hr \/>\n<h3>\u4e09\u3001\u521d\u59cb\u5316 Vite \u9879\u76ee<\/h3>\n<p>npm create vite&#064;latest cf-pages-d1-demo &#8212; &#8211;template react-ts<br \/>\ncd cf-pages-d1-demo<\/p>\n<p>npm install<br \/>\nnpm install -D wrangler &#064;cloudflare\/workers-types <\/p>\n<p>\u5efa\u8bae\u5728 package.json \u4e2d\u51c6\u5907\u4ee5\u4e0b\u811a\u672c&#xff1a;<\/p>\n<p>{<br \/>\n  &#034;scripts&#034;: {<br \/>\n    &#034;dev&#034;: &#034;vite&#034;,<br \/>\n    &#034;build&#034;: &#034;tsc &#8211;noEmit &amp;&amp; vite build&#034;,<br \/>\n    &#034;preview&#034;: &#034;vite preview&#034;,<br \/>\n    &#034;dev:pages&#034;: &#034;npm run build &amp;&amp; wrangler pages dev dist&#034;<br \/>\n  }<br \/>\n} <\/p>\n<p>Vite \u9ed8\u8ba4\u4f1a\u628a\u751f\u4ea7\u6784\u5efa\u4ea7\u7269\u8f93\u51fa\u5230 dist&#xff0c;\u800c Cloudflare Pages \u7684\u6784\u5efa\u76ee\u5f55\u9700\u8981\u586b\u5199\u5b9e\u9645\u8f93\u51fa\u76ee\u5f55\u3002(developers.cloudflare.com)<\/p>\n<hr \/>\n<h3>\u56db\u3001\u521b\u5efa D1 \u6570\u636e\u5e93<\/h3>\n<p>\u5148\u767b\u5f55 Cloudflare&#xff1a;<\/p>\n<p>npx wrangler login <\/p>\n<p>\u521b\u5efa\u751f\u4ea7\u6570\u636e\u5e93&#xff1a;<\/p>\n<p>npx wrangler d1 create cf-pages-d1-demo <\/p>\n<p>\u7ec8\u7aef\u4f1a\u8f93\u51fa\u7c7b\u4f3c\u4ee5\u4e0b\u5185\u5bb9&#xff1a;<\/p>\n<p>database_name &#061; &#034;cf-pages-d1-demo&#034;<br \/>\ndatabase_id &#061; &#034;xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx&#034; <\/p>\n<p>\u5efa\u8bae\u518d\u521b\u5efa\u4e00\u4e2a\u9884\u89c8\u6570\u636e\u5e93&#xff0c;\u907f\u514d\u6d4b\u8bd5\u5206\u652f\u6216 PR \u8bef\u5199\u5165\u6b63\u5f0f\u6570\u636e&#xff1a;<\/p>\n<p>npx wrangler d1 create cf-pages-d1-demo-preview <\/p>\n<p>\u9700\u8981\u4fdd\u5b58\u4e24\u7ec4\u4fe1\u606f&#xff1a;<\/p>\n<ul>\n<li>\u751f\u4ea7\u6570\u636e\u5e93\u540d\u79f0\u548c ID&#xff1b;<\/li>\n<li>\u9884\u89c8\u6570\u636e\u5e93\u540d\u79f0\u548c ID\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>\u4e94\u3001\u914d\u7f6e Wrangler \u548c D1 \u7ed1\u5b9a<\/h3>\n<p>\u5728\u9879\u76ee\u6839\u76ee\u5f55\u65b0\u5efa wrangler.toml&#xff1a;<\/p>\n<p>name &#061; &#034;cf-pages-d1-demo&#034;<br \/>\npages_build_output_dir &#061; &#034;.\/dist&#034;<br \/>\ncompatibility_date &#061; &#034;2026-07-31&#034;<\/p>\n<p>[[d1_databases]]<br \/>\nbinding &#061; &#034;DB&#034;<br \/>\ndatabase_name &#061; &#034;cf-pages-d1-demo&#034;<br \/>\ndatabase_id &#061; &#034;&lt;\u4f60\u7684\u751f\u4ea7\u6570\u636e\u5e93 ID&gt;&#034;<br \/>\npreview_database_id &#061; &#034;&lt;\u4f60\u7684\u9884\u89c8\u6570\u636e\u5e93 ID&gt;&#034;<br \/>\nmigrations_dir &#061; &#034;migrations&#034; <\/p>\n<p>\u8fd9\u91cc\u6700\u91cd\u8981\u7684\u662f&#xff1a;<\/p>\n<p>binding &#061; &#034;DB&#034; <\/p>\n<p>\u5b83\u51b3\u5b9a\u4e86\u540e\u7aef\u4ee3\u7801\u4e2d\u8bbf\u95ee\u6570\u636e\u5e93\u7684\u53d8\u91cf\u540d&#xff1a;<\/p>\n<p>context.env.DB <\/p>\n<p>Cloudflare Pages \u7684 D1 \u7ed1\u5b9a\u65e2\u53ef\u4ee5\u901a\u8fc7 Dashboard \u8bbe\u7f6e&#xff0c;\u4e5f\u53ef\u4ee5\u5199\u5165 Wrangler \u914d\u7f6e\u6587\u4ef6\u3002\u5bf9\u4e8e\u957f\u671f\u7ef4\u62a4\u9879\u76ee&#xff0c;\u66f4\u63a8\u8350\u628a Wrangler \u914d\u7f6e\u63d0\u4ea4\u5230 Git&#xff0c;\u8ba9\u914d\u7f6e\u548c\u4ee3\u7801\u4e00\u8d77\u7248\u672c\u5316&#xff1b;\u4e00\u65e6\u4ee5 Wrangler \u914d\u7f6e\u4f5c\u4e3a\u6765\u6e90&#xff0c;\u5c31\u5e94\u907f\u514d\u548c Dashboard \u4e2d\u7684\u540c\u7c7b\u8bbe\u7f6e\u957f\u671f\u6df7\u7528&#xff0c;\u4ee5\u514d\u51fa\u73b0\u914d\u7f6e\u4e0d\u4e00\u81f4\u3002(developers.cloudflare.com)<\/p>\n<p>\u5982\u679c\u4f60\u6682\u65f6\u4e0d\u60f3\u7ef4\u62a4 wrangler.toml&#xff0c;\u4e5f\u53ef\u4ee5\u5728 Cloudflare Dashboard \u4e2d\u8fdb\u5165&#xff1a;<\/p>\n<p>Workers &amp; Pages<br \/>\n\u2192 \u9009\u62e9\u9879\u76ee<br \/>\n\u2192 Settings<br \/>\n\u2192 Bindings<br \/>\n\u2192 Add<br \/>\n\u2192 D1 database bindings <\/p>\n<p>\u7ed1\u5b9a\u53d8\u91cf\u540d\u586b\u5199 DB&#xff0c;\u7136\u540e\u9009\u62e9\u521b\u5efa\u597d\u7684 D1 \u6570\u636e\u5e93\u3002\u5b8c\u6210\u540e\u9700\u8981\u91cd\u65b0\u90e8\u7f72&#xff0c;\u7ed1\u5b9a\u624d\u4f1a\u751f\u6548\u3002(developers.cloudflare.com)<\/p>\n<hr \/>\n<h3>\u516d\u3001\u521b\u5efa\u6570\u636e\u5e93\u8fc1\u79fb\u6587\u4ef6<\/h3>\n<p>\u4e0d\u8981\u76f4\u63a5\u5728\u751f\u4ea7\u6570\u636e\u5e93\u4e2d\u624b\u5de5\u6539\u8868\u3002\u63a8\u8350\u628a\u6570\u636e\u5e93\u7ed3\u6784\u53d8\u66f4\u5199\u6210\u8fc1\u79fb\u6587\u4ef6&#xff0c;\u5e76\u63d0\u4ea4\u5230 Git\u3002<\/p>\n<p>\u521b\u5efa\u8fc1\u79fb&#xff1a;<\/p>\n<p>npx wrangler d1 migrations create cf-pages-d1-demo initial_schema <\/p>\n<p>\u5728 migrations\/ \u76ee\u5f55\u4e2d\u5199\u5165 SQL&#xff0c;\u4f8b\u5982&#xff1a;<\/p>\n<p>&#8212; migrations\/0001_initial_schema.sql<\/p>\n<p>CREATE TABLE IF NOT EXISTS todos (<br \/>\n  id TEXT PRIMARY KEY,<br \/>\n  title TEXT NOT NULL,<br \/>\n  done INTEGER NOT NULL DEFAULT 0,<br \/>\n  created_at TEXT NOT NULL<br \/>\n);<\/p>\n<p>CREATE INDEX IF NOT EXISTS idx_todos_created_at<br \/>\nON todos(created_at DESC); <\/p>\n<p>\u5148\u5728\u672c\u5730\u6570\u636e\u5e93\u5e94\u7528\u8fc1\u79fb&#xff1a;<\/p>\n<p>npx wrangler d1 migrations apply cf-pages-d1-demo &#8211;local <\/p>\n<p>\u786e\u8ba4\u65e0\u8bef\u540e&#xff0c;\u518d\u5e94\u7528\u5230\u751f\u4ea7\u6570\u636e\u5e93&#xff1a;<\/p>\n<p>npx wrangler d1 migrations apply cf-pages-d1-demo &#8211;remote <\/p>\n<p>\u5982\u679c\u914d\u7f6e\u4e86\u9884\u89c8\u6570\u636e\u5e93&#xff0c;\u4e5f\u5e94\u540c\u6b65\u6267\u884c&#xff1a;<\/p>\n<p>npx wrangler d1 migrations apply cf-pages-d1-demo &#8211;preview <\/p>\n<p>D1 \u7684\u8fc1\u79fb\u6587\u4ef6\u4f1a\u8bb0\u5f55\u5df2\u5e94\u7528\u72b6\u6001&#xff1b;migrations apply \u4f1a\u5e94\u7528\u5c1a\u672a\u6267\u884c\u7684\u8fc1\u79fb\u3002\u5bf9\u4e8e\u751f\u4ea7\u73af\u5883&#xff0c;\u5efa\u8bae\u7528\u6570\u636e\u5e93\u540d\u79f0\u800c\u4e0d\u662f\u7ed1\u5b9a\u540d\u6267\u884c\u8fc1\u79fb&#xff0c;\u907f\u514d\u56e0\u4e3a\u7ed1\u5b9a\u540d\u88ab\u4fee\u6539\u800c\u8bef\u64cd\u4f5c\u9519\u8bef\u7684\u6570\u636e\u5e93\u3002(developers.cloudflare.com)<\/p>\n<p>\u91cd\u8981\u63d0\u9192&#xff1a; Wrangler \u7684 D1 \u547d\u4ee4\u9ed8\u8ba4\u504f\u5411\u672c\u5730\u6570\u636e\u5e93\u884c\u4e3a\u3002\u771f\u6b63\u6267\u884c\u751f\u4ea7\u8fc1\u79fb\u6216\u67e5\u8be2\u65f6&#xff0c;\u5fc5\u987b\u660e\u786e\u5e26\u4e0a &#8211;remote\u3002(developers.cloudflare.com)<\/p>\n<hr \/>\n<h3>\u4e03\u3001\u7f16\u5199 Pages Functions API<\/h3>\n<p>Cloudflare Pages \u4f1a\u6839\u636e\u6839\u76ee\u5f55 functions\/ \u4e2d\u7684\u6587\u4ef6\u8def\u5f84\u81ea\u52a8\u751f\u6210\u8def\u7531\u3002<\/p>\n<p>\u76ee\u5f55\u7ed3\u6784\u5982\u4e0b&#xff1a;<\/p>\n<p>cf-pages-d1-demo\/<br \/>\n\u251c\u2500\u2500 functions\/<br \/>\n\u2502   \u2514\u2500\u2500 api\/<br \/>\n\u2502       \u2514\u2500\u2500 todos.ts<br \/>\n\u251c\u2500\u2500 migrations\/<br \/>\n\u2502   \u2514\u2500\u2500 0001_initial_schema.sql<br \/>\n\u251c\u2500\u2500 src\/<br \/>\n\u2502   \u2514\u2500\u2500 App.tsx<br \/>\n\u251c\u2500\u2500 wrangler.toml<br \/>\n\u2514\u2500\u2500 package.json <\/p>\n<p>\u5176\u4e2d&#xff1a;<\/p>\n<p>functions\/api\/todos.ts <\/p>\n<p>\u4f1a\u81ea\u52a8\u5bf9\u5e94&#xff1a;<\/p>\n<p>\/api\/todos <\/p>\n<p>Pages Functions \u7684 functions\/ \u5fc5\u987b\u4f4d\u4e8e\u9879\u76ee\u6839\u76ee\u5f55&#xff0c;\u800c\u4e0d\u662f src\/ \u6216 dist\/ \u5185\u3002(developers.cloudflare.com)<\/p>\n<h4>\u793a\u4f8b&#xff1a;\u67e5\u8be2\u548c\u65b0\u589e\u5f85\u529e\u4e8b\u9879<\/h4>\n<p>\/\/ functions\/api\/todos.ts<\/p>\n<p>type Env &#061; {<br \/>\n  DB: D1Database;<br \/>\n};<\/p>\n<p>type Todo &#061; {<br \/>\n  id: string;<br \/>\n  title: string;<br \/>\n  done: number;<br \/>\n  created_at: string;<br \/>\n};<\/p>\n<p>export const onRequestGet: PagesFunction&lt;Env&gt; &#061; async ({ env }) &#061;&gt; {<br \/>\n  const result &#061; await env.DB<br \/>\n    .prepare(<br \/>\n      &#096;SELECT id, title, done, created_at<br \/>\n       FROM todos<br \/>\n       ORDER BY created_at DESC&#096;,<br \/>\n    )<br \/>\n    .all&lt;Todo&gt;();<\/p>\n<p>  return Response.json(result.results);<br \/>\n};<\/p>\n<p>export const onRequestPost: PagesFunction&lt;Env&gt; &#061; async ({ request, env }) &#061;&gt; {<br \/>\n  const input &#061; await request.json() as { title?: unknown };<br \/>\n  const title &#061; typeof input.title &#061;&#061;&#061; &#034;string&#034; ? input.title.trim() : &#034;&#034;;<\/p>\n<p>  if (!title || title.length &gt; 120) {<br \/>\n    return Response.json(<br \/>\n      { error: &#034;title \u5fc5\u987b\u662f 1 \u5230 120 \u4e2a\u5b57\u7b26&#034; },<br \/>\n      { status: 400 },<br \/>\n    );<br \/>\n  }<\/p>\n<p>  const todo &#061; {<br \/>\n    id: crypto.randomUUID(),<br \/>\n    title,<br \/>\n    done: 0,<br \/>\n    created_at: new Date().toISOString(),<br \/>\n  };<\/p>\n<p>  await env.DB<br \/>\n    .prepare(<br \/>\n      &#096;INSERT INTO todos (id, title, done, created_at)<br \/>\n       VALUES (?, ?, ?, ?)&#096;,<br \/>\n    )<br \/>\n    .bind(todo.id, todo.title, todo.done, todo.created_at)<br \/>\n    .run();<\/p>\n<p>  return Response.json(todo, { status: 201 });<br \/>\n}; <\/p>\n<p>D1 \u5e94\u59cb\u7ec8\u4f7f\u7528 prepare(&#8230;).bind(&#8230;) \u53c2\u6570\u7ed1\u5b9a&#xff0c;\u800c\u4e0d\u662f\u628a\u7528\u6237\u8f93\u5165\u76f4\u63a5\u62fc\u63a5\u5230 SQL \u5b57\u7b26\u4e32\u4e2d\u3002D1 \u7684 Workers Binding API \u652f\u6301 prepare\u3001bind\u3001run\u3001first\u3001all \u548c\u6279\u91cf\u6267\u884c\u7b49\u64cd\u4f5c\u3002(developers.cloudflare.com)<\/p>\n<hr \/>\n<h3>\u516b\u3001\u524d\u7aef\u8c03\u7528 API<\/h3>\n<p>\u5728 src\/App.tsx \u4e2d\u5199\u4e00\u4e2a\u6700\u5c0f\u793a\u4f8b&#xff1a;<\/p>\n<p>import { FormEvent, useEffect, useState } from &#034;react&#034;;<\/p>\n<p>type Todo &#061; {<br \/>\n  id: string;<br \/>\n  title: string;<br \/>\n  done: number;<br \/>\n  created_at: string;<br \/>\n};<\/p>\n<p>export default function App() {<br \/>\n  const [todos, setTodos] &#061; useState&lt;Todo[]&gt;([]);<br \/>\n  const [title, setTitle] &#061; useState(&#034;&#034;);<br \/>\n  const [loading, setLoading] &#061; useState(true);<\/p>\n<p>  async function loadTodos() {<br \/>\n    setLoading(true);<\/p>\n<p>    const response &#061; await fetch(&#034;\/api\/todos&#034;, {<br \/>\n      cache: &#034;no-store&#034;,<br \/>\n    });<\/p>\n<p>    if (!response.ok) {<br \/>\n      throw new Error(&#034;\u52a0\u8f7d\u5931\u8d25&#034;);<br \/>\n    }<\/p>\n<p>    setTodos(await response.json());<br \/>\n    setLoading(false);<br \/>\n  }<\/p>\n<p>  useEffect(() &#061;&gt; {<br \/>\n    void loadTodos();<br \/>\n  }, []);<\/p>\n<p>  async function addTodo(event: FormEvent) {<br \/>\n    event.preventDefault();<\/p>\n<p>    const response &#061; await fetch(&#034;\/api\/todos&#034;, {<br \/>\n      method: &#034;POST&#034;,<br \/>\n      headers: {<br \/>\n        &#034;content-type&#034;: &#034;application\/json&#034;,<br \/>\n      },<br \/>\n      body: JSON.stringify({ title }),<br \/>\n    });<\/p>\n<p>    if (!response.ok) {<br \/>\n      alert(&#034;\u65b0\u589e\u5931\u8d25&#034;);<br \/>\n      return;<br \/>\n    }<\/p>\n<p>    setTitle(&#034;&#034;);<br \/>\n    await loadTodos();<br \/>\n  }<\/p>\n<p>  return (<br \/>\n    &lt;main&gt;<br \/>\n      &lt;h1&gt;Cloudflare Pages &#043; D1 Demo&lt;\/h1&gt;<\/p>\n<p>      &lt;form onSubmit&#061;{addTodo}&gt;<br \/>\n        &lt;input<br \/>\n          value&#061;{title}<br \/>\n          placeholder&#061;&#034;\u8f93\u5165\u5f85\u529e\u4e8b\u9879&#034;<br \/>\n          onChange&#061;{(event) &#061;&gt; setTitle(event.target.value)}<br \/>\n        \/&gt;<br \/>\n        &lt;button disabled&#061;{!title.trim()} type&#061;&#034;submit&#034;&gt;<br \/>\n          \u6dfb\u52a0<br \/>\n        &lt;\/button&gt;<br \/>\n      &lt;\/form&gt;<\/p>\n<p>      {loading ? (<br \/>\n        &lt;p&gt;\u52a0\u8f7d\u4e2d\u2026&lt;\/p&gt;<br \/>\n      ) : (<br \/>\n        &lt;ul&gt;<br \/>\n          {todos.map((todo) &#061;&gt; (<br \/>\n            &lt;li key&#061;{todo.id}&gt;{todo.title}&lt;\/li&gt;<br \/>\n          ))}<br \/>\n        &lt;\/ul&gt;<br \/>\n      )}<br \/>\n    &lt;\/main&gt;<br \/>\n  );<br \/>\n} <\/p>\n<p>\u524d\u7aef\u548c Functions \u90e8\u7f72\u5728\u540c\u4e00\u4e2a Pages \u9879\u76ee\u4e2d\u65f6&#xff0c;\u8c03\u7528 \/api\/todos \u5373\u53ef&#xff0c;\u4e0d\u9700\u8981\u5199\u672c\u5730\u5f00\u53d1\u7aef\u53e3\u6216\u5355\u72ec\u7ef4\u62a4\u540e\u7aef\u57df\u540d\u3002<\/p>\n<hr \/>\n<h3>\u4e5d\u3001\u672c\u5730\u8fd0\u884c\u5b8c\u6574\u73af\u5883<\/h3>\n<p>\u4ec5\u6267\u884c&#xff1a;<\/p>\n<p>npm run dev <\/p>\n<p>\u901a\u5e38\u53ea\u80fd\u8fd0\u884c Vite \u524d\u7aef&#xff0c;\u4e0d\u80fd\u5b8c\u6574\u6a21\u62df Pages Functions \u548c D1\u3002<\/p>\n<p>\u8981\u672c\u5730\u6d4b\u8bd5\u201c\u524d\u7aef &#043; Functions &#043; D1\u201d&#xff0c;\u5148\u6784\u5efa&#xff0c;\u518d\u4f7f\u7528 Wrangler&#xff1a;<\/p>\n<p>npm run build<\/p>\n<p>npx wrangler pages dev dist &#8211;d1 DB&#061;&lt;\u4f60\u7684\u6570\u636e\u5e93 ID&gt; <\/p>\n<p>\u4e5f\u53ef\u4ee5\u5199\u5165\u811a\u672c&#xff1a;<\/p>\n<p>{<br \/>\n  &#034;scripts&#034;: {<br \/>\n    &#034;dev:pages&#034;: &#034;npm run build &amp;&amp; wrangler pages dev dist &#8211;d1 DB&#061;&lt;\u4f60\u7684\u6570\u636e\u5e93 ID&gt;&#034;<br \/>\n  }<br \/>\n} <\/p>\n<p>\u7136\u540e\u542f\u52a8&#xff1a;<\/p>\n<p>npm run dev:pages <\/p>\n<p>\u672c\u5730\u6d4f\u89c8\u5668\u8bbf\u95ee Wrangler \u8f93\u51fa\u7684\u5730\u5740&#xff0c;\u4f8b\u5982&#xff1a;<\/p>\n<p>http:\/\/127.0.0.1:8788 <\/p>\n<p>Cloudflare \u7684\u672c\u5730\u5f00\u53d1\u9ed8\u8ba4\u4f7f\u7528\u672c\u5730\u6301\u4e45\u5316\u6570\u636e&#xff0c;\u800c\u4e0d\u662f\u751f\u4ea7 D1&#xff1b;\u56e0\u6b64\u5e94\u5148\u5728\u672c\u5730\u9a8c\u8bc1\u8fc1\u79fb\u548c API&#xff0c;\u518d\u660e\u786e\u4f7f\u7528 &#8211;remote \u64cd\u4f5c\u8fdc\u7a0b\u6570\u636e\u5e93\u3002(developers.cloudflare.com)<\/p>\n<hr \/>\n<h3>\u5341\u3001\u914d\u7f6e\u73af\u5883\u53d8\u91cf\u4e0e\u5bc6\u94a5<\/h3>\n<h4>1. \u975e\u654f\u611f\u53d8\u91cf<\/h4>\n<p>\u4f8b\u5982\u8fd0\u884c\u73af\u5883\u540d\u79f0\u3001\u529f\u80fd\u5f00\u5173\u7b49&#xff1a;<\/p>\n<p>[vars]<br \/>\nAPP_ENV &#061; &#034;production&#034; <\/p>\n<p>\u5728 Function \u4e2d\u8bfb\u53d6&#xff1a;<\/p>\n<p>type Env &#061; {<br \/>\n  DB: D1Database;<br \/>\n  APP_ENV: string;<br \/>\n};<\/p>\n<p>export const onRequest: PagesFunction&lt;Env&gt; &#061; async ({ env }) &#061;&gt; {<br \/>\n  return Response.json({<br \/>\n    environment: env.APP_ENV,<br \/>\n  });<br \/>\n}; <\/p>\n<h4>2. \u654f\u611f\u53d8\u91cf<\/h4>\n<p>\u4f8b\u5982\u7ba1\u7406\u5458\u5bc6\u7801\u3001JWT \u5bc6\u94a5\u3001\u7b2c\u4e09\u65b9 API Key&#xff0c;\u5fc5\u987b\u4f7f\u7528 Secret\u3002<\/p>\n<p>\u672c\u5730\u521b\u5efa .dev.vars&#xff1a;<\/p>\n<p>ADMIN_PASSWORD&#061;&#034;replace-me&#034;<br \/>\nJWT_SECRET&#061;&#034;replace-me-too&#034; <\/p>\n<p>\u5e76\u52a0\u5165 .gitignore&#xff1a;<\/p>\n<p>.dev.vars*<br \/>\n.env* <\/p>\n<p>\u751f\u4ea7\u73af\u5883\u53ef\u4ee5\u5728 Dashboard \u4e2d\u8fdb\u5165&#xff1a;<\/p>\n<p>Workers &amp; Pages<br \/>\n\u2192 \u9879\u76ee<br \/>\n\u2192 Settings<br \/>\n\u2192 Variables and Secrets<br \/>\n\u2192 Add<br \/>\n\u2192 Encrypt <\/p>\n<p>\u4e5f\u53ef\u4ee5\u901a\u8fc7 Wrangler \u5199\u5165&#xff1a;<\/p>\n<p>npx wrangler pages secret put ADMIN_PASSWORD \\\\<br \/>\n  &#8211;project-name cf-pages-d1-demo<\/p>\n<p>npx wrangler pages secret put JWT_SECRET \\\\<br \/>\n  &#8211;project-name cf-pages-d1-demo <\/p>\n<p>Pages \u7684 Secret \u53ea\u80fd\u5728\u8fd0\u884c\u65f6\u901a\u8fc7 context.env \u8bfb\u53d6&#xff1b;\u672c\u5730\u5f00\u53d1\u53ef\u4ee5\u4f7f\u7528 .dev.vars \u6216 .env&#xff0c;\u4f46\u8fd9\u7c7b\u6587\u4ef6\u4e0d\u5e94\u63d0\u4ea4\u5230 Git\u3002(developers.cloudflare.com)<\/p>\n<hr \/>\n<h3>\u5341\u4e00\u3001\u90e8\u7f72\u5230 Cloudflare Pages&#xff1a;\u63a8\u8350 GitHub \u81ea\u52a8\u90e8\u7f72<\/h3>\n<p>\u8fd9\u662f\u6700\u9002\u5408\u957f\u671f\u9879\u76ee\u7684\u65b9\u6848\u3002<\/p>\n<h4>1. \u521d\u59cb\u5316 Git \u5e76\u63a8\u9001 GitHub<\/h4>\n<p>git init<br \/>\ngit add .<br \/>\ngit commit -m &#034;feat: initial Cloudflare Pages app&#034;<\/p>\n<p>git branch -M main<br \/>\ngit remote add origin git&#064;github.com:&lt;\u4f60\u7684\u7528\u6237\u540d&gt;\/cf-pages-d1-demo.git<br \/>\ngit push -u origin main <\/p>\n<h4>2. \u5728 Cloudflare Dashboard \u521b\u5efa Pages \u9879\u76ee<\/h4>\n<p>\u8fdb\u5165&#xff1a;<\/p>\n<p>Workers &amp; Pages<br \/>\n\u2192 Create application<br \/>\n\u2192 Pages<br \/>\n\u2192 Connect to Git <\/p>\n<p>\u7136\u540e&#xff1a;<\/p>\n<li>\n<p>\u6388\u6743 Cloudflare \u8bbf\u95ee GitHub&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u9009\u62e9\u4ed3\u5e93&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u8bbe\u7f6e\u751f\u4ea7\u5206\u652f\u4e3a main&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u8bbe\u7f6e\u6784\u5efa\u547d\u4ee4&#xff1a;<\/p>\n<p> npm run build <\/li>\n<li>\n<p>\u8bbe\u7f6e\u8f93\u51fa\u76ee\u5f55&#xff1a;<\/p>\n<p> dist <\/li>\n<li>\n<p>\u521b\u5efa\u5e76\u7b49\u5f85\u7b2c\u4e00\u6b21\u90e8\u7f72\u5b8c\u6210\u3002<\/p>\n<\/li>\n<p>\u5982\u679c\u9879\u76ee\u5df2\u5728 GitHub \u4e2d\u96c6\u6210 Pages&#xff0c;\u540e\u7eed\u63a8\u9001\u5230\u751f\u4ea7\u5206\u652f\u4f1a\u89e6\u53d1\u81ea\u52a8\u6784\u5efa\u548c\u53d1\u5e03&#xff1b;\u5176\u4ed6\u5206\u652f\u6216 PR \u53ef\u7528\u4e8e\u751f\u6210\u9884\u89c8\u90e8\u7f72\u3002(developers.cloudflare.com)<\/p>\n<h4>3. \u4e4b\u540e\u7684\u65e5\u5e38\u53d1\u5e03\u6d41\u7a0b<\/h4>\n<p>git add .<br \/>\ngit commit -m &#034;feat: add todo API&#034;<br \/>\ngit push origin main <\/p>\n<p>Cloudflare Pages \u4f1a\u81ea\u52a8\u5b8c\u6210&#xff1a;<\/p>\n<p>\u62c9\u53d6\u4ed3\u5e93<br \/>\n\u2192 \u5b89\u88c5\u4f9d\u8d56<br \/>\n\u2192 \u6267\u884c\u6784\u5efa<br \/>\n\u2192 \u90e8\u7f72\u9759\u6001\u8d44\u6e90<br \/>\n\u2192 \u90e8\u7f72 Functions<br \/>\n\u2192 \u66f4\u65b0 pages.dev \u548c\u81ea\u5b9a\u4e49\u57df\u540d <\/p>\n<hr \/>\n<h3>\u5341\u4e8c\u3001\u547d\u4ee4\u884c\u76f4\u63a5\u90e8\u7f72\u65b9\u6848<\/h3>\n<p>\u5982\u679c\u4e0d\u60f3\u7528 GitHub \u81ea\u52a8\u90e8\u7f72&#xff0c;\u4e5f\u53ef\u4ee5\u76f4\u63a5\u7528 Wrangler\u3002<\/p>\n<p>\u5148\u521b\u5efa Pages \u9879\u76ee&#xff1a;<\/p>\n<p>npx wrangler pages project create cf-pages-d1-demo \\\\<br \/>\n  &#8211;production-branch main <\/p>\n<p>\u6784\u5efa\u5e76\u90e8\u7f72&#xff1a;<\/p>\n<p>npm run build<\/p>\n<p>npx wrangler pages deploy dist \\\\<br \/>\n  &#8211;project-name cf-pages-d1-demo \\\\<br \/>\n  &#8211;branch main <\/p>\n<p>wrangler pages deploy \u53ef\u4ee5\u76f4\u63a5\u53d1\u5e03\u6784\u5efa\u76ee\u5f55&#xff1b;&#8211;project-name \u6307\u5411\u76ee\u6807 Pages \u9879\u76ee&#xff0c;&#8211;branch \u7528\u4e8e\u6307\u5b9a\u90e8\u7f72\u5206\u652f\u3002(developers.cloudflare.com)<\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f&#xff1a;<\/p>\n<ul>\n<li>\u5e26 Functions \u7684\u9879\u76ee\u4e0d\u80fd\u4f9d\u8d56 Dashboard \u7684 Direct Upload&#xff1b;<\/li>\n<li>\u82e5\u9879\u76ee\u5305\u542b functions\/&#xff0c;\u5e94\u9009\u62e9 Git \u96c6\u6210\u6216 Wrangler \u8fdb\u884c\u90e8\u7f72\u3002(developers.cloudflare.com)<\/li>\n<\/ul>\n<hr \/>\n<h3>\u5341\u4e09\u3001\u90e8\u7f72\u524d\u540e\u7684\u6570\u636e\u5e93\u6d41\u7a0b<\/h3>\n<p>\u63a8\u8350\u628a\u53d1\u5e03\u62c6\u6210\u4e24\u4e2a\u6b65\u9aa4&#xff1a;<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260802082446-6a6efece94de3.png\" \/><\/p>\n<p>\u751f\u4ea7\u53d1\u5e03\u65f6\u7684\u63a8\u8350\u547d\u4ee4\u987a\u5e8f&#xff1a;<\/p>\n<p># 1. \u67e5\u770b\u751f\u4ea7\u5e93\u5c1a\u672a\u6267\u884c\u7684\u8fc1\u79fb<br \/>\nnpx wrangler d1 migrations list cf-pages-d1-demo &#8211;remote<\/p>\n<p># 2. \u5e94\u7528\u751f\u4ea7\u8fc1\u79fb<br \/>\nnpx wrangler d1 migrations apply cf-pages-d1-demo &#8211;remote<\/p>\n<p># 3. \u67e5\u8be2\u786e\u8ba4\u8868\u7ed3\u6784<br \/>\nnpx wrangler d1 execute cf-pages-d1-demo \\\\<br \/>\n  &#8211;remote \\\\<br \/>\n  &#8211;command &#034;SELECT name FROM sqlite_schema WHERE type&#061;&#039;table&#039; ORDER BY name;&#034;<\/p>\n<p># 4. \u53d1\u5e03\u4ee3\u7801<br \/>\ngit push origin main <\/p>\n<p>D1 \u652f\u6301\u901a\u8fc7 migrations list \u67e5\u770b\u672a\u5e94\u7528\u8fc1\u79fb&#xff0c;\u5e76\u901a\u8fc7 &#8211;remote \u5bf9\u751f\u4ea7\u6570\u636e\u5e93\u6267\u884c\u8fc1\u79fb\u6216\u67e5\u8be2\u3002(developers.cloudflare.com)<\/p>\n<hr \/>\n<h3>\u5341\u56db\u3001\u81ea\u5b9a\u4e49\u57df\u540d<\/h3>\n<p>\u90e8\u7f72\u6210\u529f\u540e&#xff0c;\u9ed8\u8ba4\u4f1a\u5f97\u5230&#xff1a;<\/p>\n<p>https:\/\/&lt;\u9879\u76ee\u540d&gt;.pages.dev <\/p>\n<p>\u6dfb\u52a0\u81ea\u5df1\u7684\u57df\u540d\u65f6&#xff0c;\u8fdb\u5165&#xff1a;<\/p>\n<p>Workers &amp; Pages<br \/>\n\u2192 \u9879\u76ee<br \/>\n\u2192 Custom domains<br \/>\n\u2192 Set up a custom domain <\/p>\n<p>\u5982\u679c\u57df\u540d\u672c\u8eab\u7531 Cloudflare \u6258\u7ba1&#xff0c;\u901a\u5e38\u53ef\u4ee5\u5728 Dashboard \u4e2d\u5b8c\u6210\u5173\u8054&#xff1b;\u5982\u679c DNS \u5728\u5176\u4ed6\u5e73\u53f0&#xff0c;\u5219\u6309 Cloudflare \u7ed9\u51fa\u7684 CNAME \u6216 DNS \u63d0\u793a\u914d\u7f6e\u3002<\/p>\n<p>\u5efa\u8bae\u81f3\u5c11\u51c6\u5907&#xff1a;<\/p>\n<p>example.com<br \/>\nwww.example.com <\/p>\n<p>\u5e76\u628a\u5176\u4e2d\u4e00\u4e2a\u914d\u7f6e\u4e3a\u4e3b\u57df\u540d&#xff0c;\u53e6\u4e00\u4e2a\u505a\u8df3\u8f6c\u3002<\/p>\n<hr \/>\n<h3>\u5341\u4e94\u3001\u5e38\u89c1\u95ee\u9898\u4e0e\u6392\u67e5<\/h3>\n<h4>1. \u9875\u9762\u6b63\u5e38&#xff0c;\u4f46 \/api\/* \u8fd4\u56de 404<\/h4>\n<p>\u68c0\u67e5&#xff1a;<\/p>\n<ul>\n<li>functions\/ \u662f\u5426\u4f4d\u4e8e\u9879\u76ee\u6839\u76ee\u5f55&#xff1b;<\/li>\n<li>\u6587\u4ef6\u8def\u5f84\u662f\u5426\u6b63\u786e&#xff0c;\u4f8b\u5982 functions\/api\/todos.ts&#xff1b;<\/li>\n<li>\u662f\u5426\u91cd\u65b0\u90e8\u7f72&#xff1b;<\/li>\n<li>\u662f\u5426\u8bef\u628a Functions \u653e\u8fdb\u4e86 src\/ \u6216 dist\/\u3002<\/li>\n<\/ul>\n<p>Pages Functions \u7684\u8def\u7531\u6765\u81ea\u6839\u76ee\u5f55 functions\/ \u7684\u6587\u4ef6\u7ed3\u6784\u3002(developers.cloudflare.com)<\/p>\n<hr \/>\n<h4>2. Function \u62a5\u9519&#xff1a;Cannot read properties of undefined (reading &#039;prepare&#039;)<\/h4>\n<p>\u901a\u5e38\u8868\u793a&#xff1a;<\/p>\n<p>context.env.DB <\/p>\n<p>\u6ca1\u6709\u62ff\u5230 D1 binding\u3002<\/p>\n<p>\u68c0\u67e5&#xff1a;<\/p>\n<ul>\n<li>wrangler.toml \u7684 binding &#061; &#034;DB&#034;&#xff1b;<\/li>\n<li>\u4ee3\u7801\u662f\u5426\u540c\u6837\u4f7f\u7528 env.DB&#xff1b;<\/li>\n<li>Dashboard \u4e2d\u662f\u5426\u6b63\u786e\u7ed1\u5b9a&#xff1b;<\/li>\n<li>\u4fee\u6539 binding \u540e\u662f\u5426\u91cd\u65b0\u90e8\u7f72\u3002<\/li>\n<\/ul>\n<p>\u7ed1\u5b9a\u540d\u5fc5\u987b\u5728\u914d\u7f6e\u3001\u90e8\u7f72\u73af\u5883\u548c\u4ee3\u7801\u4e2d\u4fdd\u6301\u4e00\u81f4\u3002(developers.cloudflare.com)<\/p>\n<hr \/>\n<h4>3. \u672c\u5730\u53ef\u7528&#xff0c;\u751f\u4ea7\u73af\u5883\u63d0\u793a\u201c\u6ca1\u6709\u8868\u201d\u6216 no such table<\/h4>\n<p>\u901a\u5e38\u662f\u53ea\u6267\u884c\u4e86&#xff1a;<\/p>\n<p>npx wrangler d1 migrations apply &#8230; &#8211;local <\/p>\n<p>\u4f46\u6ca1\u6709\u6267\u884c&#xff1a;<\/p>\n<p>npx wrangler d1 migrations apply &#8230; &#8211;remote <\/p>\n<p>\u672c\u5730 D1 \u548c\u751f\u4ea7 D1 \u662f\u4e0d\u540c\u7684\u6570\u636e\u73af\u5883&#xff1b;\u751f\u4ea7\u6570\u636e\u5e93\u9700\u8981\u5355\u72ec\u5e94\u7528\u8fc1\u79fb\u3002(developers.cloudflare.com)<\/p>\n<hr \/>\n<h4>4. Dashboard \u914d\u7f6e\u548c wrangler.toml \u5bf9\u4e0d\u4e0a<\/h4>\n<p>\u4f8b\u5982&#xff1a;<\/p>\n<ul>\n<li>Dashboard \u4e2d\u7ed1\u5b9a\u4e86\u6570\u636e\u5e93 A&#xff1b;<\/li>\n<li>\u4ed3\u5e93\u91cc\u7684 wrangler.toml \u6307\u5411\u6570\u636e\u5e93 B&#xff1b;<\/li>\n<li>\u4e0b\u4e00\u6b21 Git \u90e8\u7f72\u540e&#xff0c;\u884c\u4e3a\u7a81\u7136\u6539\u53d8\u3002<\/li>\n<\/ul>\n<p>\u89e3\u51b3\u65b9\u6cd5&#xff1a;<\/p>\n<li>\u9009\u62e9 Dashboard \u6216 Wrangler \u914d\u7f6e\u4f5c\u4e3a\u4e3b\u6765\u6e90&#xff1b;<\/li>\n<li>\u63a8\u8350\u5c06 Wrangler \u914d\u7f6e\u7eb3\u5165\u7248\u672c\u63a7\u5236&#xff1b;<\/li>\n<li>\u5982\u679c\u9879\u76ee\u5df2\u6709 Dashboard \u914d\u7f6e&#xff0c;\u53ef\u4ee5\u5148\u6267\u884c&#xff1a;<\/li>\n<p>npx wrangler pages download config &lt;\u9879\u76ee\u540d&gt; <\/p>\n<p>\u4e0b\u8f7d\u5f53\u524d\u9879\u76ee\u914d\u7f6e\u540e\u518d\u8fdb\u884c\u7edf\u4e00\u7ef4\u62a4\u3002(developers.cloudflare.com)<\/p>\n<hr \/>\n<h4>5. \u628a\u5bc6\u7801\u6216 API Key \u5199\u8fdb\u524d\u7aef\u4ee3\u7801<\/h4>\n<p>\u4e0d\u8981\u8fd9\u6837\u505a&#xff1a;<\/p>\n<p>const ADMIN_PASSWORD &#061; &#034;123456&#034;; <\/p>\n<p>\u6d4f\u89c8\u5668\u6700\u7ec8\u4f1a\u4e0b\u8f7d\u524d\u7aef JavaScript&#xff0c;\u4efb\u4f55\u5199\u5728\u524d\u7aef\u5305\u91cc\u7684\u201c\u5bc6\u94a5\u201d\u90fd\u4e0d\u662f\u771f\u6b63\u7684\u79d8\u5bc6\u3002<\/p>\n<p>\u6b63\u786e\u505a\u6cd5&#xff1a;<\/p>\n<ul>\n<li>Secret \u5b58\u5728 Cloudflare&#xff1b;<\/li>\n<li>Function \u4f7f\u7528 context.env.SECRET_NAME&#xff1b;<\/li>\n<li>\u524d\u7aef\u53ea\u8c03\u7528 API&#xff0c;\u4e0d\u63a5\u89e6\u771f\u6b63\u5bc6\u94a5\u3002<\/li>\n<\/ul>\n<hr \/>\n<h4>6. D1 \u67e5\u8be2\u8d8a\u6765\u8d8a\u6162<\/h4>\n<p>\u4f18\u5148\u68c0\u67e5&#xff1a;<\/p>\n<ul>\n<li>\u662f\u5426\u4e3a WHERE\u3001JOIN\u3001ORDER BY \u5e38\u7528\u5b57\u6bb5\u5efa\u7acb\u7d22\u5f15&#xff1b;<\/li>\n<li>\u662f\u5426\u907f\u514d\u67e5\u8be2\u5168\u90e8\u6570\u636e&#xff1b;<\/li>\n<li>\u662f\u5426\u5206\u9875&#xff1b;<\/li>\n<li>\u662f\u5426\u628a\u591a\u6761\u76f8\u4e92\u4f9d\u8d56\u8f83\u4f4e\u7684 SQL \u653e\u5230\u6279\u91cf\u64cd\u4f5c\u4e2d&#xff1b;<\/li>\n<li>\u662f\u5426\u7ed9\u9ad8\u9891\u67e5\u8be2\u67e5\u770b\u6267\u884c\u8ba1\u5212\u3002<\/li>\n<\/ul>\n<p>D1 \u7684\u7d22\u5f15\u9002\u7528\u4e8e\u7ecf\u5e38\u4f5c\u4e3a\u7b5b\u9009\u6761\u4ef6\u3001\u552f\u4e00\u7ea6\u675f\u6216\u8054\u5408\u67e5\u8be2\u6761\u4ef6\u7684\u5217\u3002(developers.cloudflare.com)<\/p>\n<hr \/>\n<h3>\u5341\u516d\u3001\u751f\u4ea7\u73af\u5883\u5efa\u8bae<\/h3>\n<h4>\u6570\u636e\u5e93<\/h4>\n<ul>\n<li>\u6240\u6709\u8868\u7ed3\u6784\u53d8\u66f4\u90fd\u5fc5\u987b\u901a\u8fc7 migration&#xff1b;<\/li>\n<li>\u751f\u4ea7\u8fc1\u79fb\u524d\u5148\u5728\u672c\u5730\u548c Preview \u9a8c\u8bc1&#xff1b;<\/li>\n<li>\u4e0d\u8981\u76f4\u63a5\u5728\u7ebf\u624b\u6539\u7ed3\u6784&#xff1b;<\/li>\n<li>\u9ad8\u9891\u67e5\u8be2\u5b57\u6bb5\u5efa\u7acb\u7d22\u5f15&#xff1b;<\/li>\n<li>\u4fdd\u5b58 created_at\u3001updated_at\u3001\u64cd\u4f5c\u65e5\u5fd7\u7b49\u5ba1\u8ba1\u5b57\u6bb5\u3002<\/li>\n<\/ul>\n<h4>API<\/h4>\n<ul>\n<li>\u6240\u6709\u5199\u64cd\u4f5c\u90fd\u9a8c\u8bc1\u8f93\u5165&#xff1b;<\/li>\n<li>SQL \u4e00\u5f8b\u4f7f\u7528\u53c2\u6570\u7ed1\u5b9a&#xff1b;<\/li>\n<li>\u7ba1\u7406\u63a5\u53e3\u5fc5\u987b\u9274\u6743&#xff1b;<\/li>\n<li>\u5bf9\u5199\u64cd\u4f5c\u589e\u52a0\u5e42\u7b49\u952e\u6216\u8bf7\u6c42 ID&#xff1b;<\/li>\n<li>\u5bf9\u9519\u8bef\u8fd4\u56de\u7edf\u4e00 JSON \u683c\u5f0f\u3002<\/li>\n<\/ul>\n<h4>\u524d\u7aef<\/h4>\n<ul>\n<li>API \u8bf7\u6c42\u4f7f\u7528\u540c\u57df\u76f8\u5bf9\u8def\u5f84&#xff0c;\u5982 \/api\/todos&#xff1b;<\/li>\n<li>\u5199\u64cd\u4f5c\u5b8c\u6210\u540e\u5237\u65b0\u6216\u66f4\u65b0\u672c\u5730\u72b6\u6001&#xff1b;<\/li>\n<li>\u5bf9\u7f51\u7edc\u9519\u8bef\u3001\u52a0\u8f7d\u72b6\u6001\u3001\u7a7a\u72b6\u6001\u505a\u5904\u7406&#xff1b;<\/li>\n<li>\u4e0d\u628a\u7ba1\u7406\u5458\u5bc6\u7801\u3001\u7b2c\u4e09\u65b9\u5bc6\u94a5\u5199\u5230\u6d4f\u89c8\u5668\u4ee3\u7801\u91cc\u3002<\/li>\n<\/ul>\n<h4>\u90e8\u7f72<\/h4>\n<ul>\n<li>main \u53ea\u653e\u53ef\u4e0a\u7ebf\u4ee3\u7801&#xff1b;<\/li>\n<li>\u5f00\u53d1\u5206\u652f\u901a\u8fc7 Pages Preview \u9a8c\u8bc1&#xff1b;<\/li>\n<li>\u6570\u636e\u5e93\u8fc1\u79fb\u4e0e\u4ee3\u7801\u53d1\u5e03\u6309\u987a\u5e8f\u6267\u884c&#xff1b;<\/li>\n<li>\u6bcf\u6b21\u751f\u4ea7\u53d1\u5e03\u540e\u81f3\u5c11\u9a8c\u8bc1&#xff1a;\n<ul>\n<li>\u9996\u9875&#xff1b;<\/li>\n<li>\u4e00\u4e2a\u8bfb\u63a5\u53e3&#xff1b;<\/li>\n<li>\u4e00\u4e2a\u5199\u63a5\u53e3&#xff1b;<\/li>\n<li>\u6570\u636e\u5e93\u662f\u5426\u5199\u5165&#xff1b;<\/li>\n<li>Pages \u6784\u5efa\u65e5\u5fd7\u662f\u5426\u6210\u529f\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<hr \/>\n<h3>\u5341\u4e03\u3001\u4e0a\u7ebf\u68c0\u67e5\u6e05\u5355<\/h3>\n<p>[ ] \u524d\u7aef npm run build \u6210\u529f<br \/>\n[ ] functions\/ \u4f4d\u4e8e\u9879\u76ee\u6839\u76ee\u5f55<br \/>\n[ ] wrangler.toml \u7684 pages_build_output_dir \u6307\u5411 dist<br \/>\n[ ] D1 binding \u540d\u79f0\u4e0e\u4ee3\u7801\u4e2d\u7684 env.DB \u4e00\u81f4<br \/>\n[ ] \u672c\u5730 migration \u5df2\u901a\u8fc7<br \/>\n[ ] Preview D1 migration \u5df2\u901a\u8fc7<br \/>\n[ ] Production D1 migration \u5df2\u901a\u8fc7<br \/>\n[ ] \u6240\u6709 Secret \u5df2\u5728 Cloudflare \u914d\u7f6e<br \/>\n[ ] .dev.vars \/ .env \u672a\u63d0\u4ea4 Git<br \/>\n[ ] GitHub main \u5df2\u8fde\u63a5\u5230 Pages \u751f\u4ea7\u5206\u652f<br \/>\n[ ] Pages \u6784\u5efa\u547d\u4ee4\u4e3a npm run build<br \/>\n[ ] Pages \u8f93\u51fa\u76ee\u5f55\u4e3a dist<br \/>\n[ ] \u90e8\u7f72\u540e API \u4e0e\u6570\u636e\u5e93\u8bfb\u5199\u5df2\u9a8c\u8bc1<br \/>\n[ ] \u81ea\u5b9a\u4e49\u57df\u540d\u548c HTTPS \u5df2\u9a8c\u8bc1 <\/p>\n<hr \/>\n<h3>\u7ed3\u8bed<\/h3>\n<p>Cloudflare Pages &#043; Functions &#043; D1 \u7684\u6838\u5fc3\u4ef7\u503c\u662f\u628a\u4f20\u7edf\u7684&#xff1a;<\/p>\n<p>\u524d\u7aef\u6258\u7ba1 &#043; Node \u670d\u52a1\u7aef &#043; \u6570\u636e\u5e93 &#043; CI\/CD <\/p>\n<p>\u7b80\u5316\u4e3a&#xff1a;<\/p>\n<p>GitHub \u4ed3\u5e93<br \/>\n\u2192 Cloudflare Pages<br \/>\n\u2192 Pages Functions<br \/>\n\u2192 D1 <\/p>\n<p>\u5bf9\u4e8e\u4f5c\u54c1\u96c6\u3001\u7ba1\u7406\u540e\u53f0\u3001\u9884\u7ea6\u7cfb\u7edf\u3001\u70b9\u9910\u5de5\u5177\u3001\u5185\u5bb9\u7ba1\u7406\u3001\u5c0f\u578b SaaS \u539f\u578b\u3001\u5185\u90e8\u5de5\u5177\u7b49\u9879\u76ee&#xff0c;\u8fd9\u5957\u67b6\u6784\u901a\u5e38\u5df2\u7ecf\u8db3\u591f\u3002\u9759\u6001\u8d44\u6e90\u7531 Pages \u627f\u8f7d&#xff0c;Function \u8bf7\u6c42\u8ba1\u5165 Workers \u4f7f\u7528\u989d\u5ea6&#xff1b;\u56e0\u6b64\u6b63\u5f0f\u4e0a\u7ebf\u524d\u4ecd\u5e94\u7ed3\u5408\u9879\u76ee\u8bbf\u95ee\u91cf\u4e0e\u5f53\u524d\u5957\u9910\u9650\u5236\u8bc4\u4f30\u5bb9\u91cf\u3002(developers.cloudflare.com)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Cloudflare \u7684\u8fd9\u5957\u7ec4\u5408\u9002\u5408\u4e2d\u5c0f\u578b\u5168\u6808\u9879\u76ee&#xff1a;<br \/>\nCloudflare Pages&#xff1a;\u6258\u7ba1 React\u3001Vue\u3001Vite\u3001\u9759\u6001\u7ad9\u70b9\u7b49\u524d\u7aef\u4ea7\u7269&#xff1b;Cloudflare Pages Functions&#xff1a;\u5728\u8fb9\u7f18\u8fd0\u884c API \u4ee3\u7801&#xff0c;\u4e0d\u9700\u8981\u5355\u72ec\u8d2d\u4e70\u6216\u7ef4\u62a4\u4f20\u7edf\u670d\u52a1\u5668&#xff1b;Cloudflare D1&#xff1a;Cloudflare \u6258\u7ba1\u7684 serverless SQLite \u6570\u636e\u5e93&#xff1b;GitH<\/p>\n","protected":false},"author":2,"featured_media":89206,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[9137,3890,92,58,2441,124],"topic":[],"class_list":["post-89208","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-atomgit","tag-cloudflare","tag-github","tag-linux","tag-2441","tag-124"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u4ece\u96f6\u90e8\u7f72\u5168\u6808\u7f51\u7ad9\uff1aCloudflare Pages + Functions + D1 \u5b8c\u6574\u5b9e\u8df5 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/89208.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u4ece\u96f6\u90e8\u7f72\u5168\u6808\u7f51\u7ad9\uff1aCloudflare Pages + Functions + D1 \u5b8c\u6574\u5b9e\u8df5 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"Cloudflare \u7684\u8fd9\u5957\u7ec4\u5408\u9002\u5408\u4e2d\u5c0f\u578b\u5168\u6808\u9879\u76ee&#xff1a; Cloudflare Pages&#xff1a;\u6258\u7ba1 React\u3001Vue\u3001Vite\u3001\u9759\u6001\u7ad9\u70b9\u7b49\u524d\u7aef\u4ea7\u7269&#xff1b;Cloudflare Pages Functions&#xff1a;\u5728\u8fb9\u7f18\u8fd0\u884c API \u4ee3\u7801&#xff0c;\u4e0d\u9700\u8981\u5355\u72ec\u8d2d\u4e70\u6216\u7ef4\u62a4\u4f20\u7edf\u670d\u52a1\u5668&#xff1b;Cloudflare D1&#xff1a;Cloudflare \u6258\u7ba1\u7684 serverless SQLite \u6570\u636e\u5e93&#xff1b;GitH\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/89208.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-08-02T08:24:47+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260802082446-6a6efece29206.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/89208.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/89208.html\",\"name\":\"\u4ece\u96f6\u90e8\u7f72\u5168\u6808\u7f51\u7ad9\uff1aCloudflare Pages + Functions + D1 \u5b8c\u6574\u5b9e\u8df5 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-08-02T08:24:47+00:00\",\"dateModified\":\"2026-08-02T08:24:47+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/89208.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/89208.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/89208.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u4ece\u96f6\u90e8\u7f72\u5168\u6808\u7f51\u7ad9\uff1aCloudflare Pages + Functions + D1 \u5b8c\u6574\u5b9e\u8df5\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u4ece\u96f6\u90e8\u7f72\u5168\u6808\u7f51\u7ad9\uff1aCloudflare Pages + Functions + D1 \u5b8c\u6574\u5b9e\u8df5 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/89208.html","og_locale":"zh_CN","og_type":"article","og_title":"\u4ece\u96f6\u90e8\u7f72\u5168\u6808\u7f51\u7ad9\uff1aCloudflare Pages + Functions + D1 \u5b8c\u6574\u5b9e\u8df5 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"Cloudflare \u7684\u8fd9\u5957\u7ec4\u5408\u9002\u5408\u4e2d\u5c0f\u578b\u5168\u6808\u9879\u76ee&#xff1a; Cloudflare Pages&#xff1a;\u6258\u7ba1 React\u3001Vue\u3001Vite\u3001\u9759\u6001\u7ad9\u70b9\u7b49\u524d\u7aef\u4ea7\u7269&#xff1b;Cloudflare Pages Functions&#xff1a;\u5728\u8fb9\u7f18\u8fd0\u884c API \u4ee3\u7801&#xff0c;\u4e0d\u9700\u8981\u5355\u72ec\u8d2d\u4e70\u6216\u7ef4\u62a4\u4f20\u7edf\u670d\u52a1\u5668&#xff1b;Cloudflare D1&#xff1a;Cloudflare \u6258\u7ba1\u7684 serverless SQLite \u6570\u636e\u5e93&#xff1b;GitH","og_url":"https:\/\/www.wsisp.com\/helps\/89208.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-08-02T08:24:47+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260802082446-6a6efece29206.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"8 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/89208.html","url":"https:\/\/www.wsisp.com\/helps\/89208.html","name":"\u4ece\u96f6\u90e8\u7f72\u5168\u6808\u7f51\u7ad9\uff1aCloudflare Pages + Functions + D1 \u5b8c\u6574\u5b9e\u8df5 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-08-02T08:24:47+00:00","dateModified":"2026-08-02T08:24:47+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/89208.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/89208.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/89208.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u4ece\u96f6\u90e8\u7f72\u5168\u6808\u7f51\u7ad9\uff1aCloudflare Pages + Functions + D1 \u5b8c\u6574\u5b9e\u8df5"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/89208","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=89208"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/89208\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/89206"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=89208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=89208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=89208"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=89208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}