{"id":180942,"date":"2024-11-09T14:28:20","date_gmt":"2024-11-09T06:28:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/180942\/"},"modified":"2024-11-09T14:28:20","modified_gmt":"2024-11-09T06:28:20","slug":"%e6%b7%b1%e5%85%a5%e7%90%86%e8%a7%a3%ef%bc%9a%e4%bd%bf%e7%94%a8http%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e5%af%a6%e7%8f%be%e6%ad%a5%e9%a9%9f-http%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/180942\/","title":{"rendered":"\u6df1\u5165\u7406\u89e3\uff1a\u4f7f\u7528HTTP\u9023\u63a5\u6578\u64da\u5eab\u7684\u5be6\u73fe\u6b65\u9a5f (http\u9023\u63a5\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"%e6%b7%b1%e5%85%a5%e7%90%86%e8%a7%a3%ef%bc%9a%e4%bd%bf%e7%94%a8http%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e5%af%a6%e7%8f%be%e6%ad%a5%e9%a9%9f-GymqGczKAT\">\u6df1\u5165\u7406\u89e3\uff1a\u4f7f\u7528HTTP\u9023\u63a5\u6578\u64da\u5eab\u7684\u5be6\u73fe\u6b65\u9a5f<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0cHTTP\uff08\u8d85\u6587\u672c\u50b3\u8f38\u5354\u8b70\uff09\u5df2\u6210\u70ba\u61c9\u7528\u7a0b\u5e8f\u8207\u6578\u64da\u5eab\u4e4b\u9593\u9032\u884c\u901a\u4fe1\u7684\u91cd\u8981\u624b\u6bb5\u3002\u96a8\u8457Web\u61c9\u7528\u7a0b\u5e8f\u7684\u666e\u53ca\uff0c\u958b\u767c\u8005\u8d8a\u4f86\u8d8a\u591a\u5730\u4f7f\u7528HTTP\u4f86\u9023\u63a5\u548c\u64cd\u4f5c\u6578\u64da\u5eab\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e\u4f7f\u7528HTTP\u9023\u63a5\u6578\u64da\u5eab\u7684\u5be6\u73fe\u6b65\u9a5f\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u7406\u89e3\u9019\u4e00\u904e\u7a0b\u3002<\/p>\n<h2 id=\"1-%e7%90%86%e8%a7%a3http%e8%88%87%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e9%97%9c%e4%bf%82-GymqGczKAT\">1. \u7406\u89e3HTTP\u8207\u6578\u64da\u5eab\u7684\u95dc\u4fc2<\/h2>\n<p>HTTP\u662f\u4e00\u7a2e\u7121\u72c0\u614b\u7684\u5354\u8b70\uff0c\u4e3b\u8981\u7528\u65bc\u5ba2\u6236\u7aef\u548c\u670d\u52d9\u5668\u4e4b\u9593\u7684\u6578\u64da\u50b3\u8f38\u3002\u6578\u64da\u5eab\u5247\u662f\u7528\u65bc\u5b58\u5132\u548c\u7ba1\u7406\u6578\u64da\u7684\u7cfb\u7d71\u3002\u7576Web\u61c9\u7528\u7a0b\u5e8f\u9700\u8981\u5f9e\u6578\u64da\u5eab\u4e2d\u6aa2\u7d22\u6216\u66f4\u65b0\u6578\u64da\u6642\uff0c\u901a\u5e38\u6703\u901a\u904eHTTP\u8acb\u6c42\u4f86\u5be6\u73fe\u3002\u9019\u7a2e\u65b9\u5f0f\u7684\u512a\u52e2\u5728\u65bc\u5b83\u80fd\u5920\u8de8\u5e73\u53f0\u3001\u8de8\u8a9e\u8a00\u5730\u9032\u884c\u6578\u64da\u4ea4\u4e92\u3002<\/p>\n<h2 id=\"2-%e6%ba%96%e5%82%99%e5%b7%a5%e4%bd%9c-GymqGczKAT\">2. \u6e96\u5099\u5de5\u4f5c<\/h2>\n<p>\u5728\u958b\u59cb\u5be6\u73feHTTP\u9023\u63a5\u6578\u64da\u5eab\u4e4b\u524d\uff0c\u958b\u767c\u8005\u9700\u8981\u9032\u884c\u4e00\u4e9b\u6e96\u5099\u5de5\u4f5c\uff1a<\/p>\n<ul>\n<li><strong>\u9078\u64c7\u6578\u64da\u5eab\uff1a<\/strong>\u6839\u64da\u61c9\u7528\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u5eab\uff0c\u5982MySQL\u3001PostgreSQL\u6216MongoDB\u7b49\u3002<\/li>\n<li><strong>\u8a2d\u7f6e\u6578\u64da\u5eab\uff1a<\/strong>\u5b89\u88dd\u4e26\u914d\u7f6e\u6240\u9078\u7684\u6578\u64da\u5eab\uff0c\u78ba\u4fdd\u5176\u80fd\u5920\u6b63\u5e38\u904b\u884c\u3002<\/li>\n<li><strong>\u642d\u5efaWeb\u670d\u52d9\u5668\uff1a<\/strong>\u9078\u64c7\u5408\u9069\u7684Web\u670d\u52d9\u5668\uff08\u5982Apache\u6216Nginx\uff09\uff0c\u4e26\u914d\u7f6e\u597d\u76f8\u61c9\u7684\u74b0\u5883\u3002<\/li>\n<\/ul>\n<h2 id=\"3-%e5%af%a6%e7%8f%be%e6%ad%a5%e9%a9%9f-GymqGczKAT\">3. \u5be6\u73fe\u6b65\u9a5f<\/h2>\n<h3 id=\"3-1-%e5%89%b5%e5%bb%baapi%e6%8e%a5%e5%8f%a3-GymqGczKAT\">3.1 \u5275\u5efaAPI\u63a5\u53e3<\/h3>\n<p>\u70ba\u4e86\u901a\u904eHTTP\u9023\u63a5\u6578\u64da\u5eab\uff0c\u9996\u5148\u9700\u8981\u5275\u5efa\u4e00\u500bAPI\u63a5\u53e3\u3002\u9019\u500b\u63a5\u53e3\u5c07\u8655\u7406\u4f86\u81ea\u5ba2\u6236\u7aef\u7684HTTP\u8acb\u6c42\uff0c\u4e26\u8207\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528Node.js\u548cExpress\u6846\u67b6\u5275\u5efa\u7c21\u55aeAPI\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>const express = require('express');\nconst mysql = require('mysql');\nconst app = express();\nconst port = 3000;\n\n\/\/ \u5275\u5efa\u6578\u64da\u5eab\u9023\u63a5\nconst connection = mysql.createConnection({\n    host: 'localhost',\n    user: 'root',\n    password: 'password',\n    database: 'test_db'\n});\n\n\/\/ \u9023\u63a5\u6578\u64da\u5eab\nconnection.connect((err) =&gt; {\n    if (err) throw err;\n    console.log('Connected to database!');\n});\n\n\/\/ \u5b9a\u7fa9API\u8def\u7531\napp.get('\/api\/data', (req, res) =&gt; {\n    connection.query('SELECT * FROM data_table', (err, results) =&gt; {\n        if (err) throw err;\n        res.json(results);\n    });\n});\n\n\/\/ \u555f\u52d5\u670d\u52d9\u5668\napp.listen(port, () =&gt; {\n    console.log(`Server running at http:\/\/localhost:${port}`);\n});<\/code><\/pre>\n<h3 id=\"3-2-%e7%99%bc%e9%80%81http%e8%ab%8b%e6%b1%82-GymqGczKAT\">3.2 \u767c\u9001HTTP\u8acb\u6c42<\/h3>\n<p>\u4e00\u65e6API\u63a5\u53e3\u5275\u5efa\u5b8c\u6210\uff0c\u5ba2\u6236\u7aef\u5c31\u53ef\u4ee5\u901a\u904eHTTP\u8acb\u6c42\u4f86\u8a2a\u554f\u6578\u64da\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528JavaScript\u7684Fetch API\u767c\u9001GET\u8acb\u6c42\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>fetch('http:\/\/localhost:3000\/api\/data')\n    .then(response =&gt; response.json())\n    .then(data =&gt; console.log(data))\n    .catch(error =&gt; console.error('Error:', error));<\/code><\/pre>\n<h3 id=\"3-3-%e8%99%95%e7%90%86%e9%9f%bf%e6%87%89-GymqGczKAT\">3.3 \u8655\u7406\u97ff\u61c9<\/h3>\n<p>\u7576\u5ba2\u6236\u7aef\u767c\u9001\u8acb\u6c42\u5f8c\uff0c\u670d\u52d9\u5668\u5c07\u8655\u7406\u8a72\u8acb\u6c42\u4e26\u8fd4\u56de\u76f8\u61c9\u7684\u6578\u64da\u3002\u958b\u767c\u8005\u9700\u8981\u78ba\u4fdd\u97ff\u61c9\u7684\u683c\u5f0f\u6b63\u78ba\uff0c\u901a\u5e38\u4f7f\u7528JSON\u683c\u5f0f\u4f86\u50b3\u905e\u6578\u64da\uff0c\u56e0\u70ba\u5b83\u6613\u65bc\u89e3\u6790\u548c\u4f7f\u7528\u3002<\/p>\n<h2 id=\"4-%e5%ae%89%e5%85%a8%e6%80%a7%e8%80%83%e9%87%8f-GymqGczKAT\">4. \u5b89\u5168\u6027\u8003\u91cf<\/h2>\n<p>\u5728\u4f7f\u7528HTTP\u9023\u63a5\u6578\u64da\u5eab\u6642\uff0c\u5b89\u5168\u6027\u662f\u4e0d\u53ef\u5ffd\u8996\u7684\u554f\u984c\u3002\u958b\u767c\u8005\u61c9\u8003\u616e\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<ul>\n<li><strong>\u4f7f\u7528HTTPS\uff1a<\/strong>\u78ba\u4fdd\u6578\u64da\u5728\u50b3\u8f38\u904e\u7a0b\u4e2d\u662f\u52a0\u5bc6\u7684\uff0c\u9632\u6b62\u6578\u64da\u88ab\u7aca\u53d6\u3002<\/li>\n<li><strong>\u9a57\u8b49\u548c\u6388\u6b0a\uff1a<\/strong>\u5c0dAPI\u8acb\u6c42\u9032\u884c\u8eab\u4efd\u9a57\u8b49\uff0c\u78ba\u4fdd\u53ea\u6709\u6388\u6b0a\u7528\u6236\u80fd\u5920\u8a2a\u554f\u6578\u64da\u3002<\/li>\n<li><strong>\u9632\u6b62SQL\u6ce8\u5165\uff1a<\/strong>\u4f7f\u7528\u53c3\u6578\u5316\u67e5\u8a62\u4f86\u9632\u6b62SQL\u6ce8\u5165\u653b\u64ca\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-GymqGczKAT\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528HTTP\u9023\u63a5\u6578\u64da\u5eab\u7684\u904e\u7a0b\u6d89\u53ca\u5275\u5efaAPI\u63a5\u53e3\u3001\u767c\u9001HTTP\u8acb\u6c42\u548c\u8655\u7406\u97ff\u61c9\u7b49\u6b65\u9a5f\u3002\u901a\u904e\u6b63\u78ba\u7684\u5be6\u73fe\u548c\u5b89\u5168\u63aa\u65bd\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6709\u6548\u5730\u5229\u7528HTTP\u5354\u8b70\u4f86\u64cd\u4f5c\u6578\u64da\u5eab\uff0c\u5f9e\u800c\u63d0\u5347Web\u61c9\u7528\u7684\u6027\u80fd\u548c\u5b89\u5168\u6027\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684\u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684\u61c9\u7528\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u6216<a href=\"https:\/\/server.hk\">\u4e91\u670d\u52a1\u5668<\/a>\u4f86\u642d\u5efa\u60a8\u7684\u670d\u52d9\u5668\u74b0\u5883\uff0c\u9019\u5c07\u70ba\u60a8\u7684\u958b\u767c\u63d0\u4f9b\u7a69\u5b9a\u7684\u57fa\u790e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6df1\u5165\u7406\u89e3HTTP\u9023\u63a5\u6578\u64da\u5eab\u7684\u5be6\u73fe\u6b65\u9a5f\uff0c\u63a2\u7d22\u5982\u4f55\u6709\u6548\u5730\u4f7f\u7528HTTP\u5354\u8b70\u9032\u884c\u6578\u64da\u5eab\u4ea4\u4e92\u8207\u7ba1\u7406\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-180942","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/180942","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=180942"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/180942\/revisions"}],"predecessor-version":[{"id":180943,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/180942\/revisions\/180943"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=180942"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=180942"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=180942"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}