{"id":81248,"date":"2024-10-16T15:15:00","date_gmt":"2024-10-16T07:15:00","guid":{"rendered":"https:\/\/server.hk\/cnblog\/81248\/"},"modified":"2024-10-16T15:15:01","modified_gmt":"2024-10-16T07:15:01","slug":"node-js%e6%90%ad%e9%85%8d%e5%a4%a7%e6%95%b8%e6%93%9a%e5%ba%ab%e5%a2%9e%e5%bc%b7%e6%95%b8%e6%93%9a%e8%99%95%e7%90%86%e8%83%bd%e5%8a%9b-node-js%e5%a4%a7%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/81248\/","title":{"rendered":"Node.js\u642d\u914d\u5927\u6578\u64da\u5eab\u589e\u5f37\u6578\u64da\u8655\u7406\u80fd\u529b (Node.js\u5927\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"node-js%e6%90%ad%e9%85%8d%e5%a4%a7%e6%95%b8%e6%93%9a%e5%ba%ab%e5%a2%9e%e5%bc%b7%e6%95%b8%e6%93%9a%e8%99%95%e7%90%86%e8%83%bd%e5%8a%9b-zUOSSZEDZE\">Node.js\u642d\u914d\u5927\u6578\u64da\u5eab\u589e\u5f37\u6578\u64da\u8655\u7406\u80fd\u529b<\/h1>\n<p>\u5728\u7576\u4eca\u6578\u64da\u9a45\u52d5\u7684\u4e16\u754c\u4e2d\uff0c\u4f01\u696d\u9700\u8981\u9ad8\u6548\u7684\u6578\u64da\u8655\u7406\u80fd\u529b\u4f86\u61c9\u5c0d\u4e0d\u65b7\u589e\u9577\u7684\u6578\u64da\u91cf\u3002Node.js\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684JavaScript\u904b\u884c\u74b0\u5883\uff0c\u8207\u5927\u6578\u64da\u5eab\u7684\u7d50\u5408\u80fd\u5920\u986f\u8457\u63d0\u5347\u6578\u64da\u8655\u7406\u7684\u6027\u80fd\u548c\u6548\u7387\u3002\u672c\u6587\u5c07\u63a2\u8a0eNode.js\u5982\u4f55\u8207\u5927\u6578\u64da\u5eab\u5354\u540c\u5de5\u4f5c\uff0c\u4ee5\u589e\u5f37\u6578\u64da\u8655\u7406\u80fd\u529b\u3002<\/p>\n<h2 id=\"node-js%e7%9a%84%e7%89%b9%e6%80%a7-zUOSSZEDZE\">Node.js\u7684\u7279\u6027<\/h2>\n<p>Node.js\u662f\u4e00\u500b\u57fa\u65bc\u4e8b\u4ef6\u9a45\u52d5\u7684\u975e\u963b\u585eI\/O\u6a21\u578b\uff0c\u9019\u4f7f\u5f97\u5b83\u5728\u8655\u7406\u5927\u91cf\u4e26\u767c\u8acb\u6c42\u6642\u8868\u73fe\u51fa\u8272\u3002\u4ee5\u4e0b\u662fNode.js\u7684\u4e00\u4e9b\u4e3b\u8981\u7279\u6027\uff1a<\/p>\n<ul>\n<li><strong>\u975e\u963b\u585eI\/O\uff1a<\/strong>Node.js\u4f7f\u7528\u4e8b\u4ef6\u9a45\u52d5\u7684\u67b6\u69cb\uff0c\u80fd\u5920\u5728\u8655\u7406\u8acb\u6c42\u6642\u4e0d\u6703\u963b\u585e\u5176\u4ed6\u64cd\u4f5c\uff0c\u9019\u5c0d\u65bc\u9ad8\u4e26\u767c\u7684\u61c9\u7528\u5c24\u70ba\u91cd\u8981\u3002<\/li>\n<li><strong>\u55ae\u7dda\u7a0b\u6a21\u578b\uff1a<\/strong>\u96d6\u7136Node.js\u662f\u55ae\u7dda\u7a0b\u7684\uff0c\u4f46\u5b83\u80fd\u5920\u901a\u904e\u4e8b\u4ef6\u5faa\u74b0\u4f86\u8655\u7406\u591a\u500b\u8acb\u6c42\uff0c\u9019\u4f7f\u5f97\u5b83\u5728\u8cc7\u6e90\u4f7f\u7528\u4e0a\u66f4\u52a0\u9ad8\u6548\u3002<\/li>\n<li><strong>\u8c50\u5bcc\u7684\u751f\u614b\u7cfb\u7d71\uff1a<\/strong>Node.js\u64c1\u6709\u5927\u91cf\u7684\u7b2c\u4e09\u65b9\u6a21\u584a\u548c\u5eab\uff0c\u9019\u4e9b\u8cc7\u6e90\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u5feb\u901f\u69cb\u5efa\u61c9\u7528\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a4%a7%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e9%81%b8%e6%93%87-zUOSSZEDZE\">\u5927\u6578\u64da\u5eab\u7684\u9078\u64c7<\/h2>\n<p>\u5728\u9078\u64c7\u5927\u6578\u64da\u5eab\u6642\uff0c\u958b\u767c\u8005\u9700\u8981\u8003\u616e\u6578\u64da\u7684\u6027\u8cea\u3001\u67e5\u8a62\u7684\u8907\u96dc\u6027\u4ee5\u53ca\u7cfb\u7d71\u7684\u64f4\u5c55\u6027\u3002\u4ee5\u4e0b\u662f\u5e7e\u7a2e\u5e38\u898b\u7684\u5927\u6578\u64da\u5eab\uff1a<\/p>\n<ul>\n<li><strong>MongoDB\uff1a<\/strong>\u4f5c\u70ba\u4e00\u7a2eNoSQL\u6578\u64da\u5eab\uff0cMongoDB\u975e\u5e38\u9069\u5408\u8655\u7406\u975e\u7d50\u69cb\u5316\u6578\u64da\uff0c\u4e26\u4e14\u652f\u6301\u9ad8\u6548\u7684\u67e5\u8a62\u548c\u7d22\u5f15\u3002<\/li>\n<li><strong>Cassandra\uff1a<\/strong>\u9019\u662f\u4e00\u7a2e\u5206\u4f48\u5f0f\u6578\u64da\u5eab\uff0c\u9069\u5408\u8655\u7406\u5927\u91cf\u7684\u5beb\u5165\u8acb\u6c42\uff0c\u4e26\u4e14\u80fd\u5920\u5728\u591a\u500b\u4f3a\u670d\u5668\u4e4b\u9593\u9032\u884c\u6578\u64da\u8907\u88fd\u3002<\/li>\n<li><strong>PostgreSQL\uff1a<\/strong>\u4f5c\u70ba\u4e00\u7a2e\u95dc\u4fc2\u578b\u6578\u64da\u5eab\uff0cPostgreSQL\u652f\u6301\u8907\u96dc\u7684\u67e5\u8a62\u548c\u4e8b\u52d9\u8655\u7406\uff0c\u9069\u5408\u9700\u8981\u5f37\u4e00\u81f4\u6027\u7684\u61c9\u7528\u3002<\/li>\n<\/ul>\n<h2 id=\"node-js%e8%88%87%e5%a4%a7%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e6%95%b4%e5%90%88-zUOSSZEDZE\">Node.js\u8207\u5927\u6578\u64da\u5eab\u7684\u6574\u5408<\/h2>\n<p>Node.js\u53ef\u4ee5\u901a\u904e\u591a\u7a2e\u65b9\u5f0f\u8207\u5927\u6578\u64da\u5eab\u9032\u884c\u6574\u5408\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u6574\u5408\u65b9\u6cd5\uff1a<\/p>\n<h3 id=\"%e4%bd%bf%e7%94%a8orm%e6%a1%86%e6%9e%b6-zUOSSZEDZE\">\u4f7f\u7528ORM\u6846\u67b6<\/h3>\n<p>ORM\uff08\u5c0d\u8c61\u95dc\u4fc2\u6620\u5c04\uff09\u6846\u67b6\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u65b9\u4fbf\u5730\u8207\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\u3002\u4ee5Sequelize\u70ba\u4f8b\uff0c\u9019\u662f\u4e00\u500b\u652f\u6301\u591a\u7a2e\u6578\u64da\u5eab\u7684ORM\u6846\u67b6\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u901a\u904e\u7c21\u55ae\u7684JavaScript\u4ee3\u78bc\u4f86\u9032\u884c\u6578\u64da\u64cd\u4f5c\uff1a<\/p>\n<pre><code>const { Sequelize, DataTypes } = require('sequelize');\nconst sequelize = new Sequelize('database', 'username', 'password', {\n    host: 'localhost',\n    dialect: 'mysql'\n});\n\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\/\/ \u5275\u5efa\u4e00\u500b\u65b0\u7528\u6236\nUser.create({ username: 'example', password: 'password123' });\n<\/code><\/pre>\n<h3 id=\"%e4%bd%bf%e7%94%a8%e5%8e%9f%e7%94%9f%e9%a9%85%e5%8b%95-zUOSSZEDZE\">\u4f7f\u7528\u539f\u751f\u9a45\u52d5<\/h3>\n<p>\u5c0d\u65bc\u9700\u8981\u66f4\u9ad8\u6027\u80fd\u7684\u61c9\u7528\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u9078\u64c7\u4f7f\u7528\u6578\u64da\u5eab\u7684\u539f\u751f\u9a45\u52d5\u3002\u4f8b\u5982\uff0c\u4f7f\u7528MongoDB\u7684\u539f\u751f\u9a45\u52d5\u53ef\u4ee5\u76f4\u63a5\u9032\u884c\u6578\u64da\u64cd\u4f5c\uff1a<\/p>\n<pre><code>const { MongoClient } = require('mongodb');\nconst url = 'mongodb:\/\/localhost:27017';\nconst client = new MongoClient(url);\n\nasync function run() {\n    try {\n        await client.connect();\n        const database = client.db('testdb');\n        const collection = database.collection('users');\n        const user = { username: 'example', password: 'password123' };\n        const result = await collection.insertOne(user);\n        console.log(`New user created with the following id: ${result.insertedId}`);\n    } finally {\n        await client.close();\n    }\n}\nrun().catch(console.dir);\n<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e8%ab%96-zUOSSZEDZE\">\u7d50\u8ad6<\/h2>\n<p>Node.js\u8207\u5927\u6578\u64da\u5eab\u7684\u7d50\u5408\u70ba\u958b\u767c\u8005\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u6578\u64da\u8655\u7406\u80fd\u529b\u3002\u901a\u904e\u975e\u963b\u585eI\/O\u548c\u9ad8\u6548\u7684\u4e8b\u4ef6\u9a45\u52d5\u67b6\u69cb\uff0cNode.js\u80fd\u5920\u5728\u8655\u7406\u5927\u91cf\u6578\u64da\u6642\u4fdd\u6301\u9ad8\u6548\u3002\u9078\u64c7\u5408\u9069\u7684\u5927\u6578\u64da\u5eab\u4e26\u4f7f\u7528ORM\u6216\u539f\u751f\u9a45\u52d5\u9032\u884c\u6574\u5408\uff0c\u53ef\u4ee5\u9032\u4e00\u6b65\u63d0\u5347\u61c9\u7528\u7684\u6027\u80fd\u548c\u53ef\u64f4\u5c55\u6027\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684\u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684Node.js\u61c9\u7528\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u6216<a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a>\u4f86\u63d0\u5347\u60a8\u7684\u6578\u64da\u8655\u7406\u80fd\u529b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22Node.js\u8207\u5927\u6578\u64da\u5eab\u7684\u7d50\u5408\uff0c\u63d0\u5347\u6578\u64da\u8655\u7406\u80fd\u529b\uff0c\u5be6\u73fe\u9ad8\u6548\u80fd\u61c9\u7528\u958b\u767c\u8207\u6578\u64da\u5206\u6790\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-81248","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/81248","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=81248"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/81248\/revisions"}],"predecessor-version":[{"id":81249,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/81248\/revisions\/81249"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=81248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=81248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=81248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}