{"id":166509,"date":"2024-11-05T16:47:39","date_gmt":"2024-11-05T08:47:39","guid":{"rendered":"https:\/\/server.hk\/cnblog\/166509\/"},"modified":"2024-11-05T16:47:40","modified_gmt":"2024-11-05T08:47:40","slug":"%e5%bf%ab%e9%80%9f%e4%b8%8a%e6%89%8b%ef%bc%9a%e4%bd%bf%e7%94%a8node-js%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab-nodejs%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/166509\/","title":{"rendered":"\u5feb\u901f\u4e0a\u624b\uff1a\u4f7f\u7528Node.js\u9023\u63a5\u6578\u64da\u5eab (nodejs\u9023\u63a5\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"%e5%bf%ab%e9%80%9f%e4%b8%8a%e6%89%8b%ef%bc%9a%e4%bd%bf%e7%94%a8node-js%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab-tUnLdWybmI\">\u5feb\u901f\u4e0a\u624b\uff1a\u4f7f\u7528Node.js\u9023\u63a5\u6578\u64da\u5eab<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u958b\u767c\u4e2d\uff0cNode.js\u56e0\u5176\u9ad8\u6548\u7684\u975e\u963b\u585eI\/O\u6a21\u578b\u800c\u53d7\u5230\u5ee3\u6cdb\u6b61\u8fce\u3002\u7121\u8ad6\u662f\u69cb\u5efaAPI\u9084\u662f\u5168\u68e7\u61c9\u7528\uff0cNode.js\u90fd\u80fd\u8f15\u9b06\u61c9\u5c0d\u3002\u800c\u5728\u9019\u4e9b\u61c9\u7528\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u9023\u63a5\u8207\u64cd\u4f5c\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Node.js\u9023\u63a5\u6578\u64da\u5eab\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u7bc4\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\u3002<\/p>\n<h2 id=\"%e9%81%b8%e6%93%87%e6%95%b8%e6%93%9a%e5%ba%ab-tUnLdWybmI\">\u9078\u64c7\u6578\u64da\u5eab<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u9078\u64c7\u4e00\u500b\u5408\u9069\u7684\u6578\u64da\u5eab\u3002Node.js\u652f\u6301\u591a\u7a2e\u6578\u64da\u5eab\uff0c\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u95dc\u4fc2\u578b\u6578\u64da\u5eab\uff1a<\/strong>\u5982MySQL\u3001PostgreSQL\u7b49\u3002<\/li>\n<li><strong>\u975e\u95dc\u4fc2\u578b\u6578\u64da\u5eab\uff1a<\/strong>\u5982MongoDB\u3001Redis\u7b49\u3002<\/li>\n<\/ul>\n<p>\u6839\u64da\u61c9\u7528\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u5eab\u985e\u578b\uff0c\u63a5\u4e0b\u4f86\u6211\u5011\u5c07\u4ee5MongoDB\u548cMySQL\u70ba\u4f8b\uff0c\u4ecb\u7d39\u5982\u4f55\u5728Node.js\u4e2d\u9032\u884c\u9023\u63a5\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8mongodb-tUnLdWybmI\">\u4f7f\u7528MongoDB<\/h2>\n<p>MongoDB\u662f\u4e00\u7a2e\u6d41\u884c\u7684\u975e\u95dc\u4fc2\u578b\u6578\u64da\u5eab\uff0c\u9069\u5408\u5b58\u5132\u5927\u91cf\u7684\u6587\u6a94\u6578\u64da\u3002\u8981\u5728Node.js\u4e2d\u4f7f\u7528MongoDB\uff0c\u9996\u5148\u9700\u8981\u5b89\u88ddMongoDB\u7684Node.js\u9a45\u52d5\u3002<\/p>\n<pre><code>npm install mongodb<\/code><\/pre>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\u9023\u63a5\u5230MongoDB\u6578\u64da\u5eab\uff1a<\/p>\n<pre><code>const { MongoClient } = require('mongodb');\n\n\/\/ MongoDB\u9023\u63a5URL\nconst url = 'mongodb:\/\/localhost:27017';\nconst dbName = 'mydatabase';\n\nasync function main() {\n    const client = new MongoClient(url);\n\n    try {\n        \/\/ \u9023\u63a5\u5230MongoDB\n        await client.connect();\n        console.log('\u6210\u529f\u9023\u63a5\u5230MongoDB');\n\n        const db = client.db(dbName);\n        \/\/ \u5728\u9019\u88e1\u53ef\u4ee5\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\n    } catch (err) {\n        console.error(err);\n    } finally {\n        await client.close();\n    }\n}\n\nmain().catch(console.error);<\/code><\/pre>\n<h2 id=\"%e4%bd%bf%e7%94%a8mysql-tUnLdWybmI\">\u4f7f\u7528MySQL<\/h2>\n<p>MySQL\u662f\u4e00\u7a2e\u6d41\u884c\u7684\u95dc\u4fc2\u578b\u6578\u64da\u5eab\uff0c\u9069\u5408\u9700\u8981\u7d50\u69cb\u5316\u6578\u64da\u7684\u61c9\u7528\u3002\u8981\u5728Node.js\u4e2d\u4f7f\u7528MySQL\uff0c\u9996\u5148\u9700\u8981\u5b89\u88ddMySQL\u7684Node.js\u9a45\u52d5\u3002<\/p>\n<pre><code>npm install mysql<\/code><\/pre>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\u9023\u63a5\u5230MySQL\u6578\u64da\u5eab\uff1a<\/p>\n<pre><code>const mysql = require('mysql');\n\n\/\/ \u5275\u5efaMySQL\u9023\u63a5\nconst connection = mysql.createConnection({\n    host: 'localhost',\n    user: 'yourusername',\n    password: 'yourpassword',\n    database: 'mydatabase'\n});\n\n\/\/ \u9023\u63a5\u5230MySQL\nconnection.connect((err) =&gt; {\n    if (err) {\n        console.error('\u9023\u63a5\u5931\u6557: ' + err.stack);\n        return;\n    }\n    console.log('\u6210\u529f\u9023\u63a5\u5230MySQL\uff0cID: ' + connection.threadId);\n});\n\n\/\/ \u5728\u9019\u88e1\u53ef\u4ee5\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\n\n\/\/ \u95dc\u9589\u9023\u63a5\nconnection.end();<\/code><\/pre>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c-tUnLdWybmI\">\u6578\u64da\u5eab\u64cd\u4f5c<\/h2>\n<p>\u4e00\u65e6\u6210\u529f\u9023\u63a5\u5230\u6578\u64da\u5eab\uff0c\u5c31\u53ef\u4ee5\u9032\u884c\u5404\u7a2e\u64cd\u4f5c\uff0c\u5982\u67e5\u8a62\u3001\u63d2\u5165\u3001\u66f4\u65b0\u548c\u522a\u9664\u6578\u64da\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u57fa\u672c\u7684\u64cd\u4f5c\u793a\u4f8b\uff1a<\/p>\n<h3 id=\"mongodb%e6%93%8d%e4%bd%9c%e7%a4%ba%e4%be%8b-tUnLdWybmI\">MongoDB\u64cd\u4f5c\u793a\u4f8b<\/h3>\n<pre><code>const collection = db.collection('mycollection');\n\n\/\/ \u63d2\u5165\u6578\u64da\nawait collection.insertOne({ name: 'Alice', age: 25 });\n\n\/\/ \u67e5\u8a62\u6578\u64da\nconst results = await collection.find({}).toArray();\nconsole.log(results);<\/code><\/pre>\n<h3 id=\"mysql%e6%93%8d%e4%bd%9c%e7%a4%ba%e4%be%8b-tUnLdWybmI\">MySQL\u64cd\u4f5c\u793a\u4f8b<\/h3>\n<pre><code>\/\/ \u63d2\u5165\u6578\u64da\nconnection.query('INSERT INTO users (name, age) VALUES (?, ?)', ['Alice', 25], (error, results) =&gt; {\n    if (error) throw error;\n    console.log('\u63d2\u5165\u6210\u529f\uff0cID: ' + results.insertId);\n});\n\n\/\/ \u67e5\u8a62\u6578\u64da\nconnection.query('SELECT * FROM users', (error, results) =&gt; {\n    if (error) throw error;\n    console.log(results);\n});<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-tUnLdWybmI\">\u7e3d\u7d50<\/h2>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86\u5982\u4f55\u4f7f\u7528Node.js\u9023\u63a5MongoDB\u548cMySQL\u6578\u64da\u5eab\uff0c\u4e26\u63d0\u4f9b\u4e86\u57fa\u672c\u7684\u64cd\u4f5c\u793a\u4f8b\u3002\u7121\u8ad6\u662f\u9078\u64c7\u975e\u95dc\u4fc2\u578b\u9084\u662f\u95dc\u4fc2\u578b\u6578\u64da\u5eab\uff0cNode.js\u90fd\u80fd\u8f15\u9b06\u61c9\u5c0d\u5404\u7a2e\u6578\u64da\u5eab\u64cd\u4f5c\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u80fd\u548c\u9748\u6d3b\u6027\u7684\u61c9\u7528\uff0c\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u5eab\u548c\u6280\u8853\u68e7\u81f3\u95dc\u91cd\u8981\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\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>\u5feb\u901f\u4e0a\u624b\u6307\u5357\uff0c\u6559\u4f60\u5982\u4f55\u4f7f\u7528Node.js\u8f15\u9b06\u9023\u63a5\u6578\u64da\u5eab\uff0c\u5be6\u73fe\u9ad8\u6548\u6578\u64da\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-166509","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/166509","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=166509"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/166509\/revisions"}],"predecessor-version":[{"id":166510,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/166509\/revisions\/166510"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=166509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=166509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=166509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}