{"id":81382,"date":"2024-10-16T15:20:16","date_gmt":"2024-10-16T07:20:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/81382\/"},"modified":"2024-10-16T15:20:16","modified_gmt":"2024-10-16T07:20:16","slug":"node%e5%af%a6%e7%8f%be%e9%ab%98%e6%95%88%e6%95%b8%e6%93%9a%e5%ba%ab%e4%ba%a4%e4%ba%92%ef%bc%9a%e6%8a%80%e5%b7%a7%e8%88%87%e6%96%b9%e6%b3%95-node%e8%88%87%e6%95%b8%e6%93%9a%e5%ba%ab%e4%ba%a4%e4%ba%92","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/81382\/","title":{"rendered":"Node\u5be6\u73fe\u9ad8\u6548\u6578\u64da\u5eab\u4ea4\u4e92\uff1a\u6280\u5de7\u8207\u65b9\u6cd5 (Node\u8207\u6578\u64da\u5eab\u4ea4\u4e92)"},"content":{"rendered":"<h1 id=\"node%e5%af%a6%e7%8f%be%e9%ab%98%e6%95%88%e6%95%b8%e6%93%9a%e5%ba%ab%e4%ba%a4%e4%ba%92%ef%bc%9a%e6%8a%80%e5%b7%a7%e8%88%87%e6%96%b9%e6%b3%95-UVknPvHZOW\">Node\u5be6\u73fe\u9ad8\u6548\u6578\u64da\u5eab\u4ea4\u4e92\uff1a\u6280\u5de7\u8207\u65b9\u6cd5<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u61c9\u7528\u958b\u767c\u4e2d\uff0cNode.js\u56e0\u5176\u975e\u963b\u585eI\/O\u6a21\u578b\u548c\u4e8b\u4ef6\u9a45\u52d5\u67b6\u69cb\u800c\u53d7\u5230\u5ee3\u6cdb\u95dc\u6ce8\u3002\u9019\u4f7f\u5f97Node.js\u7279\u5225\u9069\u5408\u7528\u65bc\u9ad8\u6548\u7684\u6578\u64da\u5eab\u4ea4\u4e92\u3002\u672c\u6587\u5c07\u63a2\u8a0eNode.js\u8207\u6578\u64da\u5eab\u4ea4\u4e92\u7684\u6280\u5de7\u8207\u65b9\u6cd5\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u63d0\u5347\u61c9\u7528\u6027\u80fd\u3002<\/p>\n<h2 id=\"1-%e9%81%b8%e6%93%87%e5%90%88%e9%81%a9%e7%9a%84%e6%95%b8%e6%93%9a%e5%ba%ab-UVknPvHZOW\">1. \u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u5eab<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u5eab\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002Node.js\u53ef\u4ee5\u8207\u591a\u7a2e\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\uff0c\u5305\u62ec\u95dc\u4fc2\u578b\u6578\u64da\u5eab\uff08\u5982MySQL\u3001PostgreSQL\uff09\u548c\u975e\u95dc\u4fc2\u578b\u6578\u64da\u5eab\uff08\u5982MongoDB\u3001Redis\uff09\u3002\u6839\u64da\u61c9\u7528\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u5eab\u985e\u578b\uff0c\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u6578\u64da\u8655\u7406\u6548\u7387\u3002<\/p>\n<h2 id=\"2-%e4%bd%bf%e7%94%a8orm%e5%b7%a5%e5%85%b7-UVknPvHZOW\">2. \u4f7f\u7528ORM\u5de5\u5177<\/h2>\n<p>\u5c0d\u65bc\u95dc\u4fc2\u578b\u6578\u64da\u5eab\uff0c\u4f7f\u7528ORM\uff08\u5c0d\u8c61\u95dc\u4fc2\u6620\u5c04\uff09\u5de5\u5177\u53ef\u4ee5\u7c21\u5316\u6578\u64da\u5eab\u64cd\u4f5c\u3002\u5e38\u898b\u7684Node.js ORM\u5de5\u5177\u5305\u62ecSequelize\u548cTypeORM\u3002\u9019\u4e9b\u5de5\u5177\u63d0\u4f9b\u4e86\u7c21\u55ae\u7684API\u4f86\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u4e26\u4e14\u652f\u6301\u591a\u7a2e\u6578\u64da\u5eab\u3002<\/p>\n<pre><code>const { Sequelize, DataTypes } = require('sequelize');\n\n\/\/ \u5275\u5efa\u4e00\u500bSequelize\u5be6\u4f8b\nconst sequelize = new Sequelize('database', 'username', 'password', {\n    host: 'localhost',\n    dialect: 'mysql'\n});\n\n\/\/ \u5b9a\u7fa9\u6a21\u578b\nconst User = sequelize.define('User', {\n    username: {\n        type: DataTypes.STRING,\n        allowNull: false\n    },\n    password: {\n        type: DataTypes.STRING,\n        allowNull: false\n    }\n});\n\n\/\/ \u540c\u6b65\u6a21\u578b\nsequelize.sync();\n<\/code><\/pre>\n<h2 id=\"3-%e4%bd%bf%e7%94%a8%e9%80%a3%e6%8e%a5%e6%b1%a0-UVknPvHZOW\">3. \u4f7f\u7528\u9023\u63a5\u6c60<\/h2>\n<p>\u9023\u63a5\u6c60\u662f\u4e00\u7a2e\u7ba1\u7406\u6578\u64da\u5eab\u9023\u63a5\u7684\u6280\u8853\uff0c\u53ef\u4ee5\u63d0\u9ad8\u61c9\u7528\u7684\u6027\u80fd\u3002Node.js\u7684\u6578\u64da\u5eab\u9a45\u52d5\u7a0b\u5e8f\u901a\u5e38\u652f\u6301\u9023\u63a5\u6c60\u529f\u80fd\u3002\u901a\u904e\u4f7f\u7528\u9023\u63a5\u6c60\uff0c\u53ef\u4ee5\u91cd\u7528\u5df2\u5efa\u7acb\u7684\u9023\u63a5\uff0c\u6e1b\u5c11\u9023\u63a5\u5efa\u7acb\u7684\u958b\u92b7\u3002<\/p>\n<pre><code>const mysql = require('mysql2');\n\n\/\/ \u5275\u5efa\u9023\u63a5\u6c60\nconst pool = mysql.createPool({\n    host: 'localhost',\n    user: 'username',\n    password: 'password',\n    database: 'database',\n    waitForConnections: true,\n    connectionLimit: 10,\n    queueLimit: 0\n});\n\n\/\/ \u4f7f\u7528\u9023\u63a5\u6c60\u9032\u884c\u67e5\u8a62\npool.query('SELECT * FROM users', (err, results) =&gt; {\n    if (err) throw err;\n    console.log(results);\n});\n<\/code><\/pre>\n<h2 id=\"4-%e4%bd%bf%e7%94%a8%e7%95%b0%e6%ad%a5%e7%b7%a8%e7%a8%8b-UVknPvHZOW\">4. \u4f7f\u7528\u7570\u6b65\u7de8\u7a0b<\/h2>\n<p>Node.js\u7684\u975e\u963b\u585e\u7279\u6027\u4f7f\u5f97\u7570\u6b65\u7de8\u7a0b\u6210\u70ba\u4e00\u7a2e\u5e38\u898b\u7684\u6578\u64da\u5eab\u4ea4\u4e92\u65b9\u5f0f\u3002\u4f7f\u7528Promise\u548casync\/await\u53ef\u4ee5\u4f7f\u4ee3\u78bc\u66f4\u5177\u53ef\u8b80\u6027\uff0c\u4e26\u4e14\u80fd\u5920\u6709\u6548\u8655\u7406\u7570\u6b65\u64cd\u4f5c\u3002<\/p>\n<pre><code>async function getUsers() {\n    const [rows, fields] = await pool.query('SELECT * FROM users');\n    return rows;\n}\n\ngetUsers().then(users =&gt; {\n    console.log(users);\n}).catch(err =&gt; {\n    console.error(err);\n});\n<\/code><\/pre>\n<h2 id=\"5-%e6%95%b8%e6%93%9a%e7%b7%a9%e5%ad%98-UVknPvHZOW\">5. \u6578\u64da\u7de9\u5b58<\/h2>\n<p>\u70ba\u4e86\u63d0\u9ad8\u6578\u64da\u5eab\u7684\u6027\u80fd\uff0c\u53ef\u4ee5\u8003\u616e\u4f7f\u7528\u7de9\u5b58\u6280\u8853\u3002Redis\u662f\u4e00\u500b\u6d41\u884c\u7684\u7de9\u5b58\u89e3\u6c7a\u65b9\u6848\uff0c\u53ef\u4ee5\u7528\u4f86\u5b58\u5132\u983b\u7e41\u8a2a\u554f\u7684\u6578\u64da\uff0c\u6e1b\u5c11\u5c0d\u6578\u64da\u5eab\u7684\u8acb\u6c42\u3002<\/p>\n<pre><code>const redis = require('redis');\nconst client = redis.createClient();\n\n\/\/ \u8a2d\u7f6e\u7de9\u5b58\nclient.set('user:1', JSON.stringify(user));\n\n\/\/ \u7372\u53d6\u7de9\u5b58\nclient.get('user:1', (err, reply) =&gt; {\n    if (err) throw err;\n    const user = JSON.parse(reply);\n    console.log(user);\n});\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-UVknPvHZOW\">\u7e3d\u7d50<\/h2>\n<p>Node.js\u63d0\u4f9b\u4e86\u591a\u7a2e\u9ad8\u6548\u7684\u6578\u64da\u5eab\u4ea4\u4e92\u65b9\u6cd5\uff0c\u5305\u62ec\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u5eab\u3001\u4f7f\u7528ORM\u5de5\u5177\u3001\u9023\u63a5\u6c60\u3001\u7570\u6b65\u7de8\u7a0b\u548c\u6578\u64da\u7de9\u5b58\u7b49\u3002\u9019\u4e9b\u6280\u5de7\u4e0d\u50c5\u80fd\u63d0\u9ad8\u61c9\u7528\u7684\u6027\u80fd\uff0c\u9084\u80fd\u7c21\u5316\u958b\u767c\u904e\u7a0b\u3002\u5c0d\u65bc\u5e0c\u671b\u5728\u9999\u6e2f\u5c0b\u627e\u9ad8\u6548\u80fd\u89e3\u6c7a\u65b9\u6848\u7684\u958b\u767c\u8005\uff0c<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u548c<a href=\"https:\/\/server.hk\">\u96f2\u4f3a\u670d\u5668<\/a>\u63d0\u4f9b\u4e86\u7a69\u5b9a\u7684\u57fa\u790e\u8a2d\u65bd\uff0c\u652f\u6301\u5404\u7a2e\u61c9\u7528\u7684\u958b\u767c\u8207\u90e8\u7f72\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22Node.js\u4e2d\u9ad8\u6548\u6578\u64da\u5eab\u4ea4\u4e92\u7684\u6280\u5de7\u8207\u65b9\u6cd5\uff0c\u63d0\u5347\u61c9\u7528\u6027\u80fd\u8207\u6578\u64da\u8655\u7406\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-81382","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/81382","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=81382"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/81382\/revisions"}],"predecessor-version":[{"id":81383,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/81382\/revisions\/81383"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=81382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=81382"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=81382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}