{"id":166651,"date":"2024-11-05T18:11:40","date_gmt":"2024-11-05T10:11:40","guid":{"rendered":"https:\/\/server.hk\/cnblog\/166651\/"},"modified":"2024-11-05T18:11:41","modified_gmt":"2024-11-05T10:11:41","slug":"%e5%bf%ab%e9%80%9f%e6%8e%8c%e6%8f%a1%ef%bc%81javascript%e5%a6%82%e4%bd%95%e6%93%8d%e4%bd%9c%e5%be%8c%e5%8f%b0%e6%95%b8%e6%93%9a%e5%ba%ab-js%e6%80%8e%e9%ba%bc%e6%93%8d%e4%bd%9c%e5%be%8c%e5%8f%b0","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/166651\/","title":{"rendered":"\u5feb\u901f\u638c\u63e1\uff01JavaScript\u5982\u4f55\u64cd\u4f5c\u5f8c\u53f0\u6578\u64da\u5eab (js\u600e\u9ebc\u64cd\u4f5c\u5f8c\u53f0\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"%e5%bf%ab%e9%80%9f%e6%8e%8c%e6%8f%a1%ef%bc%81javascript%e5%a6%82%e4%bd%95%e6%93%8d%e4%bd%9c%e5%be%8c%e5%8f%b0%e6%95%b8%e6%93%9a%e5%ba%ab-XkBpNCSBLl\">\u5feb\u901f\u638c\u63e1\uff01JavaScript\u5982\u4f55\u64cd\u4f5c\u5f8c\u53f0\u6578\u64da\u5eab<\/h1>\n<p>\u5728\u73fe\u4eca\u7684\u7db2\u9801\u958b\u767c\u4e2d\uff0cJavaScript\u4e0d\u50c5\u50c5\u662f\u7528\u65bc\u524d\u7aef\u7684\u4e92\u52d5\u6548\u679c\uff0c\u5b83\u4e5f\u53ef\u4ee5\u7528\u4f86\u64cd\u4f5c\u5f8c\u53f0\u6578\u64da\u5eab\u3002\u9019\u7bc7\u6587\u7ae0\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528JavaScript\u4f86\u8207\u5f8c\u53f0\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u7bc4\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u5feb\u901f\u4e0a\u624b\u3002<\/p>\n<h2 id=\"javascript%e8%88%87%e5%be%8c%e5%8f%b0%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e4%ba%a4%e4%ba%92-XkBpNCSBLl\">JavaScript\u8207\u5f8c\u53f0\u6578\u64da\u5eab\u7684\u4ea4\u4e92<\/h2>\n<p>JavaScript\u672c\u8eab\u7121\u6cd5\u76f4\u63a5\u8207\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\uff0c\u56e0\u70ba\u5b83\u662f\u4e00\u7a2e\u5ba2\u6236\u7aef\u8a9e\u8a00\u3002\u8981\u5be6\u73fe\u9019\u4e00\u9ede\uff0c\u901a\u5e38\u9700\u8981\u901a\u904e\u4e00\u500b\u5f8c\u53f0\u670d\u52d9\u5668\u4f86\u8655\u7406\u6578\u64da\u5eab\u7684\u8acb\u6c42\u3002\u9019\u500b\u5f8c\u53f0\u670d\u52d9\u5668\u53ef\u4ee5\u4f7f\u7528\u591a\u7a2e\u6280\u8853\u4f86\u69cb\u5efa\uff0c\u4f8b\u5982Node.js\u3001PHP\u3001Python\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u6b65\u9a5f\uff1a<\/p>\n<ul>\n<li>\u7528\u6236\u5728\u524d\u7aef\u767c\u9001\u8acb\u6c42\uff08\u4f8b\u5982\uff0c\u63d0\u4ea4\u8868\u55ae\uff09<\/li>\n<li>\u5f8c\u53f0\u670d\u52d9\u5668\u63a5\u6536\u8acb\u6c42\u4e26\u8655\u7406\u6578\u64da<\/li>\n<li>\u5f8c\u53f0\u670d\u52d9\u5668\u8207\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\uff08\u67e5\u8a62\u3001\u63d2\u5165\u3001\u66f4\u65b0\u6216\u522a\u9664\u6578\u64da\uff09<\/li>\n<li>\u5f8c\u53f0\u670d\u52d9\u5668\u5c07\u7d50\u679c\u8fd4\u56de\u7d66\u524d\u7aef<\/li>\n<\/ul>\n<h2 id=\"%e4%bd%bf%e7%94%a8node-js%e5%92%8cexpress%e9%80%b2%e8%a1%8c%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c-XkBpNCSBLl\">\u4f7f\u7528Node.js\u548cExpress\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c<\/h2>\n<p>Node.js\u662f\u4e00\u500b\u6d41\u884c\u7684JavaScript\u904b\u884c\u74b0\u5883\uff0c\u901a\u5e38\u8207Express\u6846\u67b6\u4e00\u8d77\u4f7f\u7528\u4f86\u69cb\u5efa\u5f8c\u53f0\u670d\u52d9\u5668\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u7bc4\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Node.js\u548cExpress\u4f86\u64cd\u4f5cMySQL\u6578\u64da\u5eab\u3002<\/p>\n<h3 id=\"%e5%ae%89%e8%a3%9d%e6%89%80%e9%9c%80%e7%9a%84%e5%8c%85-XkBpNCSBLl\">\u5b89\u88dd\u6240\u9700\u7684\u5305<\/h3>\n<pre><code>npm install express mysql<\/code><\/pre>\n<h3 id=\"%e5%bb%ba%e7%ab%8bexpress%e6%87%89%e7%94%a8-XkBpNCSBLl\">\u5efa\u7acbExpress\u61c9\u7528<\/h3>\n<pre><code>const express = require('express');\nconst mysql = require('mysql');\nconst app = express();\nconst port = 3000;\n\n\/\/ \u8a2d\u5b9aMySQL\u9023\u63a5\nconst db = mysql.createConnection({\n    host: 'localhost',\n    user: 'your_username',\n    password: 'your_password',\n    database: 'your_database'\n});\n\n\/\/ \u9023\u63a5\u5230\u6578\u64da\u5eab\ndb.connect((err) =&gt; {\n    if (err) throw err;\n    console.log('Connected to database');\n});\n\n\/\/ \u7372\u53d6\u6578\u64da\u7684API\napp.get('\/api\/users', (req, res) =&gt; {\n    db.query('SELECT * FROM users', (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<h2 id=\"%e5%89%8d%e7%ab%af%e4%bd%bf%e7%94%a8fetch-api%e7%99%bc%e9%80%81%e8%ab%8b%e6%b1%82-XkBpNCSBLl\">\u524d\u7aef\u4f7f\u7528Fetch API\u767c\u9001\u8acb\u6c42<\/h2>\n<p>\u5728\u524d\u7aef\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528Fetch API\u4f86\u767c\u9001\u8acb\u6c42\u4e26\u7372\u53d6\u6578\u64da\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>fetch('http:\/\/localhost:3000\/api\/users')\n    .then(response =&gt; response.json())\n    .then(data =&gt; {\n        console.log(data);\n    })\n    .catch(error =&gt; console.error('Error:', error));<\/code><\/pre>\n<h2 id=\"%e5%ae%89%e5%85%a8%e6%80%a7%e8%80%83%e9%87%8f-XkBpNCSBLl\">\u5b89\u5168\u6027\u8003\u91cf<\/h2>\n<p>\u5728\u64cd\u4f5c\u5f8c\u53f0\u6578\u64da\u5eab\u6642\uff0c\u5b89\u5168\u6027\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5efa\u8b70\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528\u53c3\u6578\u5316\u67e5\u8a62\u4f86\u9632\u6b62SQL\u6ce8\u5165\u653b\u64ca\u3002<\/li>\n<li>\u5c0d\u7528\u6236\u8f38\u5165\u9032\u884c\u9a57\u8b49\u548c\u6e05\u7406\u3002<\/li>\n<li>\u4f7f\u7528HTTPS\u4f86\u52a0\u5bc6\u6578\u64da\u50b3\u8f38\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-XkBpNCSBLl\">\u7e3d\u7d50<\/h2>\n<p>JavaScript\u53ef\u4ee5\u901a\u904e\u5f8c\u53f0\u670d\u52d9\u5668\u8207\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\uff0c\u9019\u4f7f\u5f97\u958b\u767c\u8005\u80fd\u5920\u5275\u5efa\u52d5\u614b\u548c\u4e92\u52d5\u7684\u61c9\u7528\u7a0b\u5e8f\u3002\u4f7f\u7528Node.js\u548cExpress\u6846\u67b6\u662f\u4e00\u7a2e\u6d41\u884c\u7684\u9078\u64c7\uff0c\u4e26\u4e14\u53ef\u4ee5\u8f15\u9b06\u5730\u8207MySQL\u7b49\u6578\u64da\u5eab\u9032\u884c\u96c6\u6210\u3002\u78ba\u4fdd\u5728\u958b\u767c\u904e\u7a0b\u4e2d\u8003\u616e\u5b89\u5168\u6027\uff0c\u4ee5\u4fdd\u8b77\u6578\u64da\u548c\u7528\u6236\u4fe1\u606f\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u53ef\u9760\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u6258\u7ba1\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u5ba2\u6236\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u985e\u578b\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a>\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u670d\u52d9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5feb\u901f\u638c\u63e1JavaScript\u64cd\u4f5c\u5f8c\u53f0\u6578\u64da\u5eab\u7684\u6280\u5de7\uff0c\u5b78\u7fd2\u5982\u4f55\u6709\u6548\u9023\u63a5\u3001\u67e5\u8a62\u548c\u7ba1\u7406\u6578\u64da\uff0c\u63d0\u5347\u958b\u767c\u6548\u7387\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-166651","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/166651","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=166651"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/166651\/revisions"}],"predecessor-version":[{"id":166652,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/166651\/revisions\/166652"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=166651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=166651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=166651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}