{"id":188588,"date":"2024-11-12T00:02:28","date_gmt":"2024-11-11T16:02:28","guid":{"rendered":"https:\/\/server.hk\/cnblog\/188588\/"},"modified":"2024-11-12T00:02:29","modified_gmt":"2024-11-11T16:02:29","slug":"%e6%8e%a2%e7%b4%a2node-js%e4%b8%ad%e7%9a%84%e6%95%b8%e6%93%9a%e5%ba%ab%e6%a1%86%e6%9e%b6%ef%bc%8c%e8%ae%93%e5%be%8c%e7%ab%af%e9%96%8b%e7%99%bc%e6%9b%b4%e9%ab%98%e6%95%88-node-js-%e6%95%b8%e6%93%9a","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/188588\/","title":{"rendered":"\u63a2\u7d22Node.js\u4e2d\u7684\u6578\u64da\u5eab\u6846\u67b6\uff0c\u8b93\u5f8c\u7aef\u958b\u767c\u66f4\u9ad8\u6548 (node.js \u6578\u64da\u5eab\u6846\u67b6)"},"content":{"rendered":"<h1 id=\"%e6%8e%a2%e7%b4%a2node-js%e4%b8%ad%e7%9a%84%e6%95%b8%e6%93%9a%e5%ba%ab%e6%a1%86%e6%9e%b6%ef%bc%8c%e8%ae%93%e5%be%8c%e7%ab%af%e9%96%8b%e7%99%bc%e6%9b%b4%e9%ab%98%e6%95%88-tNjzAPuMHf\">\u63a2\u7d22Node.js\u4e2d\u7684\u6578\u64da\u5eab\u6846\u67b6\uff0c\u8b93\u5f8c\u7aef\u958b\u767c\u66f4\u9ad8\u6548<\/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\u95dc\u6ce8\u3002\u96a8\u8457\u61c9\u7528\u7a0b\u5e8f\u7684\u8907\u96dc\u6027\u589e\u52a0\uff0c\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u5eab\u6846\u67b6\u6210\u70ba\u5f8c\u7aef\u958b\u767c\u4e2d\u7684\u4e00\u500b\u91cd\u8981\u8003\u91cf\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5e7e\u500b\u6d41\u884c\u7684Node.js\u6578\u64da\u5eab\u6846\u67b6\uff0c\u4e26\u5206\u6790\u5b83\u5011\u5982\u4f55\u63d0\u9ad8\u958b\u767c\u6548\u7387\u3002<\/p>\n<h2 id=\"1-sequelize-tNjzAPuMHf\">1. Sequelize<\/h2>\n<p>Sequelize\u662f\u4e00\u500b\u57fa\u65bcPromise\u7684Node.js ORM\uff08\u5c0d\u8c61\u95dc\u4fc2\u6620\u5c04\uff09\u6846\u67b6\uff0c\u652f\u6301\u591a\u7a2e\u6578\u64da\u5eab\uff0c\u5305\u62ecPostgreSQL\u3001MySQL\u3001MariaDB\u3001SQLite\u548cMicrosoft SQL Server\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u500b\u7c21\u55ae\u7684API\u4f86\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u4e26\u652f\u6301\u6a21\u578b\u5b9a\u7fa9\u3001\u95dc\u806f\u3001\u4e8b\u52d9\u7b49\u529f\u80fd\u3002<\/p>\n<h3 id=\"%e7%89%b9%e9%bb%9e-tNjzAPuMHf\">\u7279\u9ede<\/h3>\n<ul>\n<li>\u652f\u6301\u591a\u7a2e\u6578\u64da\u5eab<\/li>\n<li>\u5f37\u5927\u7684\u6a21\u578b\u5b9a\u7fa9\u529f\u80fd<\/li>\n<li>\u652f\u6301\u4e8b\u52d9\u548c\u95dc\u806f\u67e5\u8a62<\/li>\n<\/ul>\n<h3 id=\"%e7%a4%ba%e4%be%8b%e4%bb%a3%e7%a2%bc-tNjzAPuMHf\">\u793a\u4f8b\u4ee3\u78bc<\/h3>\n<pre><code>\nconst { 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\/\/ \u540c\u6b65\u6a21\u578b\nsequelize.sync();\n<\/code><\/pre>\n<h2 id=\"2-mongoose-tNjzAPuMHf\">2. Mongoose<\/h2>\n<p>Mongoose\u662f\u4e00\u500bMongoDB\u7684\u5c0d\u8c61\u5efa\u6a21\u5de5\u5177\uff0c\u63d0\u4f9b\u4e86\u4e00\u500b\u7c21\u55ae\u7684\u65b9\u5f0f\u4f86\u8207MongoDB\u9032\u884c\u4ea4\u4e92\u3002\u5b83\u652f\u6301\u6578\u64da\u9a57\u8b49\u3001\u67e5\u8a62\u69cb\u5efa\u548c\u4e2d\u9593\u4ef6\u7b49\u529f\u80fd\uff0c\u7279\u5225\u9069\u5408\u9700\u8981\u4f7f\u7528NoSQL\u6578\u64da\u5eab\u7684\u61c9\u7528\u3002<\/p>\n<h3 id=\"%e7%89%b9%e9%bb%9e-tNjzAPuMHf\">\u7279\u9ede<\/h3>\n<ul>\n<li>\u7c21\u55ae\u6613\u7528\u7684API<\/li>\n<li>\u652f\u6301\u6578\u64da\u9a57\u8b49\u548c\u4e2d\u9593\u4ef6<\/li>\n<li>\u5f37\u5927\u7684\u67e5\u8a62\u69cb\u5efa\u529f\u80fd<\/li>\n<\/ul>\n<h3 id=\"%e7%a4%ba%e4%be%8b%e4%bb%a3%e7%a2%bc-tNjzAPuMHf\">\u793a\u4f8b\u4ee3\u78bc<\/h3>\n<pre><code>\nconst mongoose = require('mongoose');\nmongoose.connect('mongodb:\/\/localhost\/test', { useNewUrlParser: true, useUnifiedTopology: true });\n\nconst userSchema = new mongoose.Schema({\n    username: String,\n    password: String\n});\n\nconst User = mongoose.model('User', userSchema);\n\n\/\/ \u5275\u5efa\u65b0\u7528\u6236\nconst newUser = new User({ username: 'testuser', password: 'password123' });\nnewUser.save();\n<\/code><\/pre>\n<h2 id=\"3-typeorm-tNjzAPuMHf\">3. TypeORM<\/h2>\n<p>TypeORM\u662f\u4e00\u500b\u652f\u6301TypeScript\u548cJavaScript\u7684ORM\u6846\u67b6\uff0c\u652f\u6301\u591a\u7a2e\u6578\u64da\u5eab\uff0c\u5305\u62ecMySQL\u3001PostgreSQL\u3001SQLite\u548cMongoDB\u3002\u5b83\u7684\u8a2d\u8a08\u7406\u5ff5\u662f\u7c21\u5316\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u4e26\u63d0\u4f9b\u5f37\u5927\u7684\u529f\u80fd\u4f86\u652f\u6301\u8907\u96dc\u7684\u6578\u64da\u6a21\u578b\u3002<\/p>\n<h3 id=\"%e7%89%b9%e9%bb%9e-tNjzAPuMHf\">\u7279\u9ede<\/h3>\n<ul>\n<li>\u652f\u6301TypeScript<\/li>\n<li>\u5f37\u5927\u7684\u67e5\u8a62\u529f\u80fd<\/li>\n<li>\u652f\u6301\u591a\u7a2e\u6578\u64da\u5eab<\/li>\n<\/ul>\n<h3 id=\"%e7%a4%ba%e4%be%8b%e4%bb%a3%e7%a2%bc-tNjzAPuMHf\">\u793a\u4f8b\u4ee3\u78bc<\/h3>\n<pre><code>\nimport { Entity, PrimaryGeneratedColumn, Column, createConnection } from 'typeorm';\n\n@Entity()\nclass User {\n    @PrimaryGeneratedColumn()\n    id: number;\n\n    @Column()\n    username: string;\n\n    @Column()\n    password: string;\n}\n\ncreateConnection().then(async connection =&gt; {\n    const user = new User();\n    user.username = 'testuser';\n    user.password = 'password123';\n    await connection.manager.save(user);\n});\n<\/code><\/pre>\n<h2 id=\"4-knex-js-tNjzAPuMHf\">4. Knex.js<\/h2>\n<p>Knex.js\u662f\u4e00\u500bSQL\u67e5\u8a62\u69cb\u5efa\u5668\uff0c\u652f\u6301\u591a\u7a2e\u6578\u64da\u5eab\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u500b\u9748\u6d3b\u7684API\u4f86\u69cb\u5efa\u67e5\u8a62\uff0c\u4e26\u652f\u6301\u4e8b\u52d9\u3001\u9023\u63a5\u6c60\u7b49\u529f\u80fd\u3002Knex.js\u9069\u5408\u9700\u8981\u76f4\u63a5\u64cd\u4f5cSQL\u7684\u958b\u767c\u8005\u3002<\/p>\n<h3 id=\"%e7%89%b9%e9%bb%9e-tNjzAPuMHf\">\u7279\u9ede<\/h3>\n<ul>\n<li>\u9748\u6d3b\u7684\u67e5\u8a62\u69cb\u5efa<\/li>\n<li>\u652f\u6301\u591a\u7a2e\u6578\u64da\u5eab<\/li>\n<li>\u652f\u6301\u4e8b\u52d9\u548c\u9023\u63a5\u6c60<\/li>\n<\/ul>\n<h3 id=\"%e7%a4%ba%e4%be%8b%e4%bb%a3%e7%a2%bc-tNjzAPuMHf\">\u793a\u4f8b\u4ee3\u78bc<\/h3>\n<pre><code>\nconst knex = require('knex')({\n    client: 'mysql',\n    connection: {\n        host: 'localhost',\n        user: 'username',\n        password: 'password',\n        database: 'test'\n    }\n});\n\n\/\/ \u63d2\u5165\u6578\u64da\nknex('users').insert({ username: 'testuser', password: 'password123' }).then(() =&gt; {\n    console.log('User added');\n});\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-tNjzAPuMHf\">\u7e3d\u7d50<\/h2>\n<p>\u9078\u64c7\u5408\u9069\u7684Node.js\u6578\u64da\u5eab\u6846\u67b6\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u5f8c\u7aef\u958b\u767c\u7684\u6548\u7387\u3002\u7121\u8ad6\u662f\u4f7f\u7528Sequelize\u9032\u884c\u95dc\u4fc2\u578b\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u9084\u662f\u4f7f\u7528Mongoose\u8655\u7406MongoDB\uff0c\u6216\u662f\u9078\u64c7TypeORM\u548cKnex.js\u9032\u884c\u66f4\u9748\u6d3b\u7684\u6578\u64da\u64cd\u4f5c\uff0c\u958b\u767c\u8005\u90fd\u80fd\u6839\u64da\u9805\u76ee\u7684\u9700\u6c42\u9078\u64c7\u6700\u5408\u9069\u7684\u5de5\u5177\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684\u5f8c\u7aef\u89e3\u6c7a\u65b9\u6848\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u4f86\u90e8\u7f72\u60a8\u7684Node.js\u61c9\u7528\uff0c\u4eab\u53d7\u7a69\u5b9a\u548c\u9ad8\u6548\u7684\u670d\u52d9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22Node.js\u4e2d\u7684\u6578\u64da\u5eab\u6846\u67b6\uff0c\u63d0\u5347\u5f8c\u7aef\u958b\u767c\u6548\u7387\uff0c\u7c21\u5316\u6578\u64da\u7ba1\u7406\uff0c\u5be6\u73fe\u5feb\u901f\u3001\u9748\u6d3b\u7684\u61c9\u7528\u7a0b\u5f0f\u958b\u767c\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-188588","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/188588","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=188588"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/188588\/revisions"}],"predecessor-version":[{"id":188589,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/188588\/revisions\/188589"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=188588"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=188588"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=188588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}