{"id":193459,"date":"2024-11-12T22:21:23","date_gmt":"2024-11-12T14:21:23","guid":{"rendered":"https:\/\/server.hk\/cnblog\/193459\/"},"modified":"2024-11-12T22:21:25","modified_gmt":"2024-11-12T14:21:25","slug":"%e5%ad%b8%e7%bf%92nodejs%ef%bc%8c%e6%8e%8c%e6%8f%a1%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%e6%b1%a0%e7%9a%84%e4%bd%bf%e7%94%a8%e6%96%b9%e6%b3%95-nodejs%e4%bd%bf%e7%94%a8%e6%95%b8%e6%93%9a","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/193459\/","title":{"rendered":"\u5b78\u7fd2NodeJs\uff0c\u638c\u63e1\u6578\u64da\u5eab\u9023\u63a5\u6c60\u7684\u4f7f\u7528\u65b9\u6cd5 (nodejs\u4f7f\u7528\u6578\u64da\u5eab\u9023\u63a5\u6c60)"},"content":{"rendered":"<h1 id=\"%e5%ad%b8%e7%bf%92nodejs%ef%bc%8c%e6%8e%8c%e6%8f%a1%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%e6%b1%a0%e7%9a%84%e4%bd%bf%e7%94%a8%e6%96%b9%e6%b3%95-IWlnYLRbSF\">\u5b78\u7fd2NodeJs\uff0c\u638c\u63e1\u6578\u64da\u5eab\u9023\u63a5\u6c60\u7684\u4f7f\u7528\u65b9\u6cd5<\/h1>\n<p>\u5728\u73fe\u4eca\u7684\u7db2\u7d61\u61c9\u7528\u958b\u767c\u4e2d\uff0cNode.js\u56e0\u5176\u975e\u963b\u585e\u7684I\/O\u6a21\u578b\u548c\u9ad8\u6548\u7684\u6027\u80fd\u800c\u53d7\u5230\u5ee3\u6cdb\u7684\u6b61\u8fce\u3002\u7576\u6d89\u53ca\u5230\u6578\u64da\u5eab\u64cd\u4f5c\u6642\uff0c\u5982\u4f55\u6709\u6548\u5730\u7ba1\u7406\u6578\u64da\u5eab\u9023\u63a5\u6210\u70ba\u4e86\u958b\u767c\u8005\u9700\u8981\u9762\u5c0d\u7684\u4e00\u500b\u91cd\u8981\u8ab2\u984c\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0eNode.js\u4e2d\u6578\u64da\u5eab\u9023\u63a5\u6c60\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u63d0\u5347\u61c9\u7528\u7684\u6027\u80fd\u548c\u7a69\u5b9a\u6027\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%e6%b1%a0%ef%bc%9f-IWlnYLRbSF\">\u4ec0\u9ebc\u662f\u6578\u64da\u5eab\u9023\u63a5\u6c60\uff1f<\/h2>\n<p>\u6578\u64da\u5eab\u9023\u63a5\u6c60\u662f\u4e00\u7a2e\u7ba1\u7406\u6578\u64da\u5eab\u9023\u63a5\u7684\u6280\u8853\uff0c\u5b83\u901a\u904e\u91cd\u7528\u5df2\u5efa\u7acb\u7684\u9023\u63a5\u4f86\u63d0\u9ad8\u61c9\u7528\u7684\u6027\u80fd\u3002\u7576\u61c9\u7528\u9700\u8981\u8a2a\u554f\u6578\u64da\u5eab\u6642\uff0c\u9023\u63a5\u6c60\u6703\u63d0\u4f9b\u4e00\u500b\u53ef\u7528\u7684\u9023\u63a5\uff0c\u800c\u4e0d\u662f\u6bcf\u6b21\u90fd\u5275\u5efa\u65b0\u7684\u9023\u63a5\u3002\u9019\u6a23\u4e0d\u50c5\u6e1b\u5c11\u4e86\u9023\u63a5\u7684\u958b\u92b7\uff0c\u9084\u80fd\u6709\u6548\u5730\u63a7\u5236\u540c\u6642\u9023\u63a5\u6578\u91cf\uff0c\u907f\u514d\u56e0\u70ba\u904e\u591a\u7684\u9023\u63a5\u5c0e\u81f4\u6578\u64da\u5eab\u6027\u80fd\u4e0b\u964d\u3002<\/p>\n<h2 id=\"node-js%e4%b8%ad%e4%bd%bf%e7%94%a8%e9%80%a3%e6%8e%a5%e6%b1%a0%e7%9a%84%e5%84%aa%e5%8b%a2-IWlnYLRbSF\">Node.js\u4e2d\u4f7f\u7528\u9023\u63a5\u6c60\u7684\u512a\u52e2<\/h2>\n<ul>\n<li><strong>\u6027\u80fd\u63d0\u5347\uff1a<\/strong>\u91cd\u7528\u9023\u63a5\u53ef\u4ee5\u986f\u8457\u6e1b\u5c11\u9023\u63a5\u5efa\u7acb\u7684\u6642\u9593\uff0c\u5f9e\u800c\u63d0\u9ad8\u61c9\u7528\u7684\u97ff\u61c9\u901f\u5ea6\u3002<\/li>\n<li><strong>\u8cc7\u6e90\u7ba1\u7406\uff1a<\/strong>\u901a\u904e\u9650\u5236\u540c\u6642\u9023\u63a5\u7684\u6578\u91cf\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u6578\u64da\u5eab\u8cc7\u6e90\uff0c\u907f\u514d\u904e\u8f09\u3002<\/li>\n<li><strong>\u7c21\u5316\u4ee3\u78bc\uff1a<\/strong>\u4f7f\u7528\u9023\u63a5\u6c60\u53ef\u4ee5\u7c21\u5316\u6578\u64da\u5eab\u64cd\u4f5c\u7684\u4ee3\u78bc\uff0c\u8b93\u958b\u767c\u8005\u5c08\u6ce8\u65bc\u696d\u52d9\u908f\u8f2f\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e5%9c%a8node-js%e4%b8%ad%e5%af%a6%e7%8f%be%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%e6%b1%a0-IWlnYLRbSF\">\u5982\u4f55\u5728Node.js\u4e2d\u5be6\u73fe\u6578\u64da\u5eab\u9023\u63a5\u6c60<\/h2>\n<p>\u5728Node.js\u4e2d\uff0c\u4f7f\u7528\u9023\u63a5\u6c60\u7684\u65b9\u5f0f\u53d6\u6c7a\u65bc\u6240\u4f7f\u7528\u7684\u6578\u64da\u5eab\u9a45\u52d5\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528MySQL\u548cPostgreSQL\u7684\u9023\u63a5\u6c60\u793a\u4f8b\u3002<\/p>\n<h3 id=\"%e4%bd%bf%e7%94%a8mysql%e7%9a%84%e9%80%a3%e6%8e%a5%e6%b1%a0-IWlnYLRbSF\">\u4f7f\u7528MySQL\u7684\u9023\u63a5\u6c60<\/h3>\n<pre><code>const mysql = require('mysql');\n\n\/\/ \u5275\u5efa\u9023\u63a5\u6c60\nconst pool = mysql.createPool({\n    connectionLimit: 10, \/\/ \u6700\u5927\u9023\u63a5\u6578\n    host: 'localhost',\n    user: 'yourUsername',\n    password: 'yourPassword',\n    database: 'yourDatabase'\n});\n\n\/\/ \u4f7f\u7528\u9023\u63a5\u6c60\u9032\u884c\u67e5\u8a62\npool.getConnection((err, connection) =&gt; {\n    if (err) throw err; \/\/ \u7372\u53d6\u9023\u63a5\u5931\u6557\n\n    connection.query('SELECT * FROM yourTable', (error, results) =&gt; {\n        \/\/ \u91cb\u653e\u9023\u63a5\n        connection.release();\n\n        if (error) throw error; \/\/ \u67e5\u8a62\u5931\u6557\n        console.log(results); \/\/ \u8f38\u51fa\u67e5\u8a62\u7d50\u679c\n    });\n});<\/code><\/pre>\n<h3 id=\"%e4%bd%bf%e7%94%a8postgresql%e7%9a%84%e9%80%a3%e6%8e%a5%e6%b1%a0-IWlnYLRbSF\">\u4f7f\u7528PostgreSQL\u7684\u9023\u63a5\u6c60<\/h3>\n<pre><code>const { Pool } = require('pg');\n\n\/\/ \u5275\u5efa\u9023\u63a5\u6c60\nconst pool = new Pool({\n    max: 10, \/\/ \u6700\u5927\u9023\u63a5\u6578\n    host: 'localhost',\n    user: 'yourUsername',\n    password: 'yourPassword',\n    database: 'yourDatabase'\n});\n\n\/\/ \u4f7f\u7528\u9023\u63a5\u6c60\u9032\u884c\u67e5\u8a62\npool.query('SELECT * FROM yourTable', (err, res) =&gt; {\n    if (err) throw err; \/\/ \u67e5\u8a62\u5931\u6557\n    console.log(res.rows); \/\/ \u8f38\u51fa\u67e5\u8a62\u7d50\u679c\n});\n\n\/\/ \u95dc\u9589\u9023\u63a5\u6c60\npool.end();<\/code><\/pre>\n<h2 id=\"%e6%9c%80%e4%bd%b3%e5%af%a6%e8%b8%90-IWlnYLRbSF\">\u6700\u4f73\u5be6\u8e10<\/h2>\n<p>\u5728\u4f7f\u7528\u6578\u64da\u5eab\u9023\u63a5\u6c60\u6642\uff0c\u958b\u767c\u8005\u61c9\u9075\u5faa\u4ee5\u4e0b\u6700\u4f73\u5be6\u8e10\uff1a<\/p>\n<ul>\n<li><strong>\u9069\u7576\u914d\u7f6e\u9023\u63a5\u6c60\uff1a<\/strong>\u6839\u64da\u61c9\u7528\u7684\u9700\u6c42\u548c\u6578\u64da\u5eab\u7684\u6027\u80fd\uff0c\u5408\u7406\u8a2d\u7f6e\u6700\u5927\u9023\u63a5\u6578\u3002<\/li>\n<li><strong>\u932f\u8aa4\u8655\u7406\uff1a<\/strong>\u5728\u7372\u53d6\u9023\u63a5\u548c\u57f7\u884c\u67e5\u8a62\u6642\uff0c\u52d9\u5fc5\u9032\u884c\u932f\u8aa4\u8655\u7406\uff0c\u4ee5\u907f\u514d\u61c9\u7528\u5d29\u6f70\u3002<\/li>\n<li><strong>\u5b9a\u671f\u91cb\u653e\u8cc7\u6e90\uff1a<\/strong>\u5728\u4e0d\u518d\u9700\u8981\u9023\u63a5\u6c60\u6642\uff0c\u61c9\u53ca\u6642\u95dc\u9589\u9023\u63a5\u6c60\u4ee5\u91cb\u653e\u8cc7\u6e90\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-IWlnYLRbSF\">\u7e3d\u7d50<\/h2>\n<p>\u638c\u63e1Node.js\u4e2d\u7684\u6578\u64da\u5eab\u9023\u63a5\u6c60\u4f7f\u7528\u65b9\u6cd5\uff0c\u4e0d\u50c5\u80fd\u63d0\u5347\u61c9\u7528\u7684\u6027\u80fd\uff0c\u9084\u80fd\u6709\u6548\u7ba1\u7406\u6578\u64da\u5eab\u8cc7\u6e90\u3002\u901a\u904e\u5408\u7406\u914d\u7f6e\u548c\u4f7f\u7528\u9023\u63a5\u6c60\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5c08\u6ce8\u65bc\u696d\u52d9\u908f\u8f2f\uff0c\u63d0\u5347\u958b\u767c\u6548\u7387\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u90e8\u7f72\u60a8\u7684Node.js\u61c9\u7528\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2NodeJs\uff0c\u638c\u63e1\u6578\u64da\u5eab\u9023\u63a5\u6c60\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u63d0\u5347\u61c9\u7528\u6027\u80fd\u8207\u6548\u7387\uff0c\u5be6\u73fe\u9ad8\u6548\u7684\u6578\u64da\u5eab\u64cd\u4f5c\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-193459","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/193459","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=193459"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/193459\/revisions"}],"predecessor-version":[{"id":193460,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/193459\/revisions\/193460"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=193459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=193459"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=193459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}