{"id":82,"date":"2026-06-16T10:47:32","date_gmt":"2026-06-16T02:47:32","guid":{"rendered":"https:\/\/yykcj.com\/?p=82"},"modified":"2026-06-16T10:47:32","modified_gmt":"2026-06-16T02:47:32","slug":"restful-api-%e8%ae%be%e8%ae%a1%e8%a7%84%e8%8c%83%e4%b8%8e%e6%9c%80%e4%bd%b3%e5%ae%9e%e8%b7%b5%ef%bc%9a%e4%bb%8e%e7%90%86%e8%ae%ba%e5%88%b0%e5%ae%9e%e6%88%98","status":"publish","type":"post","link":"https:\/\/yykcj.com\/?p=82","title":{"rendered":"RESTful API \u8bbe\u8ba1\u89c4\u8303\u4e0e\u6700\u4f73\u5b9e\u8df5\uff1a\u4ece\u7406\u8bba\u5230\u5b9e\u6218"},"content":{"rendered":"<h1>RESTful API \u8bbe\u8ba1\u89c4\u8303\u4e0e\u6700\u4f73\u5b9e\u8df5\uff1a\u4ece\u7406\u8bba\u5230\u5b9e\u6218<\/h1>\n<h2>\u6458\u8981<\/h2>\n<p>RESTful API\u5df2\u6210\u4e3a\u73b0\u4ee3Web\u670d\u52a1\u7684\u6807\u51c6\u67b6\u6784\u98ce\u683c\u3002\u672c\u6587\u5c06\u4ece\u8d44\u6e90\u547d\u540d\u3001HTTP\u65b9\u6cd5\u3001\u72b6\u6001\u7801\u3001\u7248\u672c\u63a7\u5236\u3001\u5b89\u5168\u7b49\u65b9\u9762\uff0c\u7ed3\u5408Node.js+Express\u5b9e\u6218\uff0c\u5e26\u4f60\u638c\u63e1RESTful API\u7684\u8bbe\u8ba1\u7cbe\u9ad3\u3002<\/p>\n<h2>1. \u5f15\u8a00<\/h2>\n<p>\u4f60\u662f\u5426\u9047\u5230\u8fc7\u8fd9\u6837\u7684API\uff1a<code>\/getUser?id=1<\/code>\u3001<code>\/deleteUser?id=2<\/code>\u3001<code>\/createUser<\/code>\uff1f\u8fd9\u79cdRPC\u98ce\u683c\u7684\u63a5\u53e3\u4e0d\u4ec5\u6df7\u4e71\uff0c\u800c\u4e14\u96be\u4ee5\u7ef4\u62a4\u3002RESTful API\u901a\u8fc7\u8d44\u6e90\u5bfc\u5411\u548c\u6807\u51c6HTTP\u65b9\u6cd5\uff0c\u63d0\u4f9b\u4e86\u7edf\u4e00\u3001\u53ef\u9884\u6d4b\u7684\u63a5\u53e3\u8bbe\u8ba1\u3002\u672c\u6587\u4e0d\u4ec5\u4f1a\u5217\u51fa\u89c4\u8303\uff0c\u8fd8\u4f1a\u901a\u8fc7\u4e00\u4e2a\u7b80\u5355\u7684\u4efb\u52a1\u7ba1\u7406\u7cfb\u7edf\uff0c\u624b\u628a\u624b\u6559\u4f60\u5b9e\u73b0RESTful API\u3002<\/p>\n<h2>2. \u6838\u5fc3\u539f\u5219<\/h2>\n<h3>2.1 \u8d44\u6e90\u5bfc\u5411<\/h3>\n<p>URL\u4ee3\u8868\u8d44\u6e90\uff08\u540d\u8bcd\uff09\uff0c\u800c\u975e\u64cd\u4f5c\uff08\u52a8\u8bcd\uff09\u3002\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>\u9519\u8bef\uff1a<code>\/getUser<\/code>\u3001<code>\/createUser<\/code><\/li>\n<li>\u6b63\u786e\uff1a<code>\/users<\/code>\u3001<code>\/users\/123<\/code><\/li>\n<\/ul>\n<h3>2.2 \u4f7f\u7528\u6807\u51c6HTTP\u65b9\u6cd5<\/h3>\n<p>| \u65b9\u6cd5 | \u64cd\u4f5c | \u793a\u4f8b | |&#8212;&#8212;|&#8212;&#8212;|&#8212;&#8212;| | GET | \u83b7\u53d6\u8d44\u6e90 | <code>GET \/users<\/code>\u3001<code>GET \/users\/123<\/code> | | POST | \u521b\u5efa\u8d44\u6e90 | <code>POST \/users<\/code> | | PUT | \u5b8c\u5168\u66ff\u6362\u8d44\u6e90 | <code>PUT \/users\/123<\/code> | | PATCH | \u90e8\u5206\u66f4\u65b0\u8d44\u6e90 | <code>PATCH \/users\/123<\/code> | | DELETE | \u5220\u9664\u8d44\u6e90 | <code>DELETE \/users\/123<\/code> |<\/p>\n<h3>2.3 \u72b6\u6001\u7801\u8868\u8fbe\u7ed3\u679c<\/h3>\n<ul>\n<li>2xx \u6210\u529f\uff1a200 OK, 201 Created, 204 No Content<\/li>\n<li>3xx \u91cd\u5b9a\u5411\uff1a301 Moved Permanently, 304 Not Modified<\/li>\n<li>4xx \u5ba2\u6237\u7aef\u9519\u8bef\uff1a400 Bad Request, 401 Unauthorized, 403 Forbidden, 404 Not Found, 409 Conflict, 422 Unprocessable Entity<\/li>\n<li>5xx \u670d\u52a1\u5668\u9519\u8bef\uff1a500 Internal Server Error, 502 Bad Gateway<\/li>\n<\/ul>\n<h2>3. \u5b9e\u6218\uff1a\u6784\u5efa\u4efb\u52a1\u7ba1\u7406API<\/h2>\n<p>\u6211\u4eec\u5c06\u4f7f\u7528Node.js + Express + \u5185\u5b58\u5b58\u50a8\u6765\u6f14\u793a\u3002<\/p>\n<h3>3.1 \u9879\u76ee\u521d\u59cb\u5316<\/h3>\n<pre><code class=\"language-bash\">\nmkdir task-api &amp;&amp; cd task-api\nnpm init -y\nnpm install express\n<\/code><\/pre>\n<h3>3.2 \u57fa\u7840\u670d\u52a1\u5668<\/h3>\n<pre><code class=\"language-javascript\">\n\/\/ server.js\nconst express = require(&#x27;express&#x27;);\nconst app = express();\napp.use(express.json());\n\nlet tasks = [];\nlet idCounter = 1;\n\napp.listen(3000, () =&gt; console.log(&#x27;API running on port 3000&#x27;));\n<\/code><\/pre>\n<h3>3.3 \u8d44\u6e90\u547d\u540d\u4e0e\u7aef\u70b9\u8bbe\u8ba1<\/h3>\n<p>| \u7aef\u70b9 | \u65b9\u6cd5 | \u63cf\u8ff0 | |&#8212;&#8212;|&#8212;&#8212;|&#8212;&#8212;| | <code>\/tasks<\/code> | GET | \u83b7\u53d6\u6240\u6709\u4efb\u52a1 | | <code>\/tasks<\/code> | POST | \u521b\u5efa\u65b0\u4efb\u52a1 | | <code>\/tasks\/:id<\/code> | GET | \u83b7\u53d6\u5355\u4e2a\u4efb\u52a1 | | <code>\/tasks\/:id<\/code> | PUT | \u5b8c\u5168\u66ff\u6362\u4efb\u52a1 | | <code>\/tasks\/:id<\/code> | PATCH | \u90e8\u5206\u66f4\u65b0\u4efb\u52a1 | | <code>\/tasks\/:id<\/code> | DELETE | \u5220\u9664\u4efb\u52a1 |<\/p>\n<h3>3.4 \u5b9e\u73b0CRUD<\/h3>\n<pre><code class=\"language-javascript\">\n\/\/ \u83b7\u53d6\u6240\u6709\u4efb\u52a1\napp.get(&#x27;\/tasks&#x27;, (req, res) =&gt; {\n  res.json(tasks);\n});\n\n\/\/ \u521b\u5efa\u4efb\u52a1\napp.post(&#x27;\/tasks&#x27;, (req, res) =&gt; {\n  const { title, completed } = req.body;\n  if (!title || typeof title !== &#x27;string&#x27;) {\n    return res.status(400).json({ error: &#x27;Title is required and must be a string&#x27; });\n  }\n  const task = {\n    id: idCounter++,\n    title,\n    completed: completed || false,\n    createdAt: new Date().toISOString()\n  };\n  tasks.push(task);\n  res.status(201).json(task);\n});\n\n\/\/ \u83b7\u53d6\u5355\u4e2a\u4efb\u52a1\napp.get(&#x27;\/tasks\/:id&#x27;, (req, res) =&gt; {\n  const id = parseInt(req.params.id);\n  const task = tasks.find(t =&gt; t.id === id);\n  if (!task) return res.status(404).json({ error: &#x27;Task not found&#x27; });\n  res.json(task);\n});\n\n\/\/ \u5b8c\u5168\u66ff\u6362\u4efb\u52a1\napp.put(&#x27;\/tasks\/:id&#x27;, (req, res) =&gt; {\n  const id = parseInt(req.params.id);\n  const index = tasks.findIndex(t =&gt; t.id === id);\n  if (index === -1) return res.status(404).json({ error: &#x27;Task not found&#x27; });\n  const { title, completed } = req.body;\n  if (!title || typeof title !== &#x27;string&#x27;) {\n    return res.status(400).json({ error: &#x27;Title is required&#x27; });\n  }\n  tasks[index] = {\n    id,\n    title,\n    completed: !!completed,\n    createdAt: tasks[index].createdAt,\n    updatedAt: new Date().toISOString()\n  };\n  res.json(tasks[index]);\n});\n\n\/\/ \u90e8\u5206\u66f4\u65b0\u4efb\u52a1\napp.patch(&#x27;\/tasks\/:id&#x27;, (req, res) =&gt; {\n  const id = parseInt(req.params.id);\n  const task = tasks.find(t =&gt; t.id === id);\n  if (!task) return res.status(404).json({ error: &#x27;Task not found&#x27; });\n  const { title, completed } = req.body;\n  if (title !== undefined) {\n    if (typeof title !== &#x27;string&#x27;) return res.status(400).json({ error: &#x27;Title must be a string&#x27; });\n    task.title = title;\n  }\n  if (completed !== undefined) {\n    if (typeof completed !== &#x27;boolean&#x27;) return res.status(400).json({ error: &#x27;Completed must be a boolean&#x27; });\n    task.completed = completed;\n  }\n  task.updatedAt = new Date().toISOString();\n  res.json(task);\n});\n\n\/\/ \u5220\u9664\u4efb\u52a1\napp.delete(&#x27;\/tasks\/:id&#x27;, (req, res) =&gt; {\n  const id = parseInt(req.params.id);\n  const index = tasks.findIndex(t =&gt; t.id === id);\n  if (index === -1) return res.status(404).json({ error: &#x27;Task not found&#x27; });\n  tasks.splice(index, 1);\n  res.status(204).send();\n});\n<\/code><\/pre>\n<p>\ud83d\udca1 <strong>\u6ce8\u610f<\/strong>\uff1aPUT\u5e94\u66ff\u6362\u6574\u4e2a\u8d44\u6e90\uff0c\u56e0\u6b64\u9700\u8981\u63d0\u4f9b\u6240\u6709\u5fc5\u586b\u5b57\u6bb5\uff1bPATCH\u53ea\u66f4\u65b0\u63d0\u4f9b\u7684\u5b57\u6bb5\u3002<\/p>\n<h3>3.5 \u6d4b\u8bd5API<\/h3>\n<p>\u4f7f\u7528curl\u6d4b\u8bd5\uff1a<\/p>\n<pre><code class=\"language-bash\">\n# \u521b\u5efa\u4efb\u52a1\ncurl -X POST http:\/\/localhost:3000\/tasks -H &quot;Content-Type: application\/json&quot; -d &#x27;{&quot;title&quot;:&quot;\u5b66\u4e60REST&quot;}&#x27;\n\n# \u83b7\u53d6\u6240\u6709\u4efb\u52a1\ncurl http:\/\/localhost:3000\/tasks\n\n# \u83b7\u53d6\u5355\u4e2a\u4efb\u52a1\ncurl http:\/\/localhost:3000\/tasks\/1\n\n# \u90e8\u5206\u66f4\u65b0\ncurl -X PATCH http:\/\/localhost:3000\/tasks\/1 -H &quot;Content-Type: application\/json&quot; -d &#x27;{&quot;completed&quot;:true}&#x27;\n\n# \u5220\u9664\u4efb\u52a1\ncurl -X DELETE http:\/\/localhost:3000\/tasks\/1\n<\/code><\/pre>\n<h2>4. \u9ad8\u7ea7\u5b9e\u8df5<\/h2>\n<h3>4.1 \u8fc7\u6ee4\u3001\u6392\u5e8f\u4e0e\u5206\u9875<\/h3>\n<p>\u4f7f\u7528\u67e5\u8be2\u53c2\u6570\uff1a<\/p>\n<pre><code class=\"language-javascript\">\n\/\/ GET \/tasks?completed=true&amp;sort=-createdAt&amp;page=1&amp;limit=10\napp.get(&#x27;\/tasks&#x27;, (req, res) =&gt; {\n  let result = [...tasks];\n  \n  \/\/ \u8fc7\u6ee4\n  if (req.query.completed !== undefined) {\n    const completed = req.query.completed === &#x27;true&#x27;;\n    result = result.filter(t =&gt; t.completed === completed);\n  }\n  \n  \/\/ \u6392\u5e8f\n  if (req.query.sort) {\n    const fields = req.query.sort.split(&#x27;,&#x27;);\n    fields.forEach(field =&gt; {\n      const order = field.startsWith(&#x27;-&#x27;) ? -1 : 1;\n      const key = field.replace(\/^-\/, &#x27;&#x27;);\n      result.sort((a, b) =&gt; (a[key] &gt; b[key] ? order : -order));\n    });\n  }\n  \n  \/\/ \u5206\u9875\n  const page = parseInt(req.query.page) || 1;\n  const limit = parseInt(req.query.limit) || 10;\n  const start = (page - 1) * limit;\n  const paginated = result.slice(start, start + limit);\n  \n  res.json({\n    data: paginated,\n    total: result.length,\n    page,\n    limit\n  });\n});\n<\/code><\/pre>\n<h3>4.2 \u7248\u672c\u63a7\u5236<\/h3>\n<p>\u63a8\u8350\u4f7f\u7528URL\u8def\u5f84\u7248\u672c\uff1a<\/p>\n<pre><code class=\"language-javascript\">\napp.use(&#x27;\/v1&#x27;, v1Router);\napp.use(&#x27;\/v2&#x27;, v2Router);\n<\/code><\/pre>\n<p>\u6216\u8005\u8bf7\u6c42\u5934\u7248\u672c\uff08\u66f4\u4f18\u96c5\u4f46\u4e0d\u6613\u53d1\u73b0\uff09\uff1a<\/p>\n<pre><code class=\"language-javascript\">\napp.use((req, res, next) =&gt; {\n  const version = req.headers[&#x27;accept-version&#x27;];\n  req.apiVersion = version || &#x27;1&#x27;;\n  next();\n});\n<\/code><\/pre>\n<h3>4.3 \u9519\u8bef\u5904\u7406<\/h3>\n<p>\u7edf\u4e00\u9519\u8bef\u54cd\u5e94\u683c\u5f0f\uff1a<\/p>\n<pre><code class=\"language-javascript\">\n\/\/ \u5168\u5c40\u9519\u8bef\u5904\u7406\u4e2d\u95f4\u4ef6\napp.use((err, req, res, next) =&gt; {\n  console.error(err.stack);\n  res.status(500).json({\n    error: &#x27;Internal Server Error&#x27;,\n    message: process.env.NODE_ENV === &#x27;development&#x27; ? err.message : &#x27;Something went wrong&#x27;\n  });\n});\n<\/code><\/pre>\n<h3>4.4 \u8ba4\u8bc1\u4e0e\u6388\u6743<\/h3>\n<p>\u4f7f\u7528JWT\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-javascript\">\nconst jwt = require(&#x27;jsonwebtoken&#x27;);\n\n\/\/ \u4e2d\u95f4\u4ef6\u9a8c\u8bc1\nfunction authenticate(req, res, next) {\n  const token = req.headers.authorization?.split(&#x27; &#x27;)[1];\n  if (!token) return res.status(401).json({ error: &#x27;No token provided&#x27; });\n  try {\n    const decoded = jwt.verify(token, process.env.JWT_SECRET);\n    req.user = decoded;\n    next();\n  } catch (err) {\n    res.status(403).json({ error: &#x27;Invalid token&#x27; });\n  }\n}\n\napp.get(&#x27;\/tasks&#x27;, authenticate, (req, res) =&gt; {\n  \/\/ \u53ea\u8fd4\u56de\u5f53\u524d\u7528\u6237\u7684\u4efb\u52a1\n  const userTasks = tasks.filter(t =&gt; t.userId === req.user.id);\n  res.json(userTasks);\n});\n<\/code><\/pre>\n<h3>4.5 \u5e42\u7b49\u6027<\/h3>\n<p>PUT\u548cDELETE\u5e94\u8be5\u662f\u5e42\u7b49\u7684\u3002\u4f8b\u5982\uff0c\u591a\u6b21DELETE\u540c\u4e00\u4e2a\u8d44\u6e90\u5e94\u8fd4\u56de\u76f8\u540c\u7ed3\u679c\uff08404\uff09\u3002<\/p>\n<h2>5. \u5e38\u89c1\u9677\u9631<\/h2>\n<ul>\n<li><strong>\u4f7f\u7528\u52a8\u8bcd<\/strong>\uff1a\u4e0d\u8981\u7528<code>\/getTasks<\/code>\uff0c\u7528<code>GET \/tasks<\/code><\/li>\n<li><strong>\u5ffd\u7565\u72b6\u6001\u7801<\/strong>\uff1a\u603b\u662f\u8fd4\u56de\u9002\u5f53\u7684\u72b6\u6001\u7801\uff0c\u4e0d\u8981\u6240\u6709\u8bf7\u6c42\u90fd\u8fd4\u56de200<\/li>\n<li><strong>\u5d4c\u5957\u8fc7\u6df1<\/strong>\uff1a\u907f\u514d<code>\/users\/1\/tasks\/2\/comments<\/code>\uff0c\u8003\u8651\u6241\u5e73\u5316\u6216\u4f7f\u7528\u67e5\u8be2\u53c2\u6570<\/li>\n<li><strong>\u5ffd\u7565HATEOAS<\/strong>\uff1a\u867d\u7136\u4e0d\u662f\u5f3a\u5236\uff0c\u4f46\u8fd4\u56de\u76f8\u5173\u94fe\u63a5\u53ef\u4ee5\u63d0\u9ad8API\u7684\u53ef\u53d1\u73b0\u6027<\/li>\n<\/ul>\n<h2>6. \u603b\u7ed3<\/h2>\n<p>\u672c\u6587\u4ece\u7406\u8bba\u5230\u5b9e\u6218\uff0c\u5e26\u4f60\u6784\u5efa\u4e86\u7b26\u5408RESTful\u539f\u5219\u7684\u4efb\u52a1\u7ba1\u7406API\u3002\u5173\u952e\u70b9\uff1a<\/p>\n<ul>\n<li>\u8d44\u6e90\u547d\u540d\u4f7f\u7528\u590d\u6570\u540d\u8bcd<\/li>\n<li>\u5229\u7528HTTP\u65b9\u6cd5\u8868\u8fbe\u64cd\u4f5c<\/li>\n<li>\u6b63\u786e\u4f7f\u7528\u72b6\u6001\u7801<\/li>\n<li>\u5b9e\u73b0\u8fc7\u6ee4\u3001\u6392\u5e8f\u3001\u5206\u9875<\/li>\n<li>\u7248\u672c\u63a7\u5236\u4e0e\u5b89\u5168<\/li>\n<\/ul>\n<p>\u4e0b\u4e00\u6b65\uff0c\u4f60\u53ef\u4ee5\u8003\u8651\u6dfb\u52a0HATEOAS\u94fe\u63a5\uff0c\u6216\u8005\u4f7f\u7528OpenAPI\u89c4\u8303\u6587\u6863\u5316API\u3002<\/p>\n<h2>\u5ef6\u4f38\u9605\u8bfb<\/h2>\n<ul>\n<li><a href=\"https:\/\/restfulapi.net\/\">RESTful API Design &#8211; \u5b98\u65b9\u6307\u5357<\/a><\/li>\n<li><a href=\"https:\/\/jsonapi.org\/\">JSON:API \u89c4\u8303<\/a><\/li>\n<li><a href=\"https:\/\/swagger.io\/specification\/\">OpenAPI \u89c4\u8303<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u6df1\u5165\u63a2\u8ba8RESTful API\u8bbe\u8ba1\u89c4\u8303\uff0c\u6db5\u76d6\u8d44\u6e90\u547d\u540d\u3001HTTP\u65b9\u6cd5\u4f7f\u7528\u3001\u72b6\u6001\u7801\u9009\u62e9\u3001\u7248\u672c\u63a7\u5236\u53ca\u5b89\u5168\u5b9e\u8df5\uff0c\u5e76\u901a\u8fc7Node.js+Express\u793a\u4f8b\u6f14\u793a\u5982\u4f55\u6784\u5efa\u7b26\u5408REST\u539f\u5219\u7684API\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[43,44,5,42,45],"class_list":["post-82","post","type-post","status-publish","format-standard","hentry","category-3","category-4","tag-api","tag-express","tag-node-js","tag-restful","tag-45"],"_links":{"self":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/82","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=82"}],"version-history":[{"count":0,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/82\/revisions"}],"wp:attachment":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=82"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=82"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=82"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}