{"id":282,"date":"2025-11-11T22:06:40","date_gmt":"2025-11-11T14:06:40","guid":{"rendered":"https:\/\/www.linerroom.cn\/?p=282"},"modified":"2026-01-21T01:41:02","modified_gmt":"2026-01-20T17:41:02","slug":"node-js-expressapi%e5%ad%a6%e4%b9%a0%e7%ac%94%e8%ae%b0","status":"publish","type":"post","link":"https:\/\/www.linerroom.cn\/?p=282","title":{"rendered":"node.js-Express(api\u5b66\u4e60\u7b14\u8bb0)-\u6301\u7eed\u66f4\u65b0"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/www.linerroom.cn\/wp-content\/uploads\/2025\/11\/image-32-1024x538.png'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"538\" data-attachment-id=\"283\" data-permalink=\"https:\/\/www.linerroom.cn\/?attachment_id=283\" data-orig-file=\"https:\/\/i0.wp.com\/www.linerroom.cn\/wp-content\/uploads\/2025\/11\/image-32.png?fit=1908%2C1003&amp;ssl=1\" data-orig-size=\"1908,1003\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/www.linerroom.cn\/wp-content\/uploads\/2025\/11\/image-32.png?fit=300%2C158&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/www.linerroom.cn\/wp-content\/uploads\/2025\/11\/image-32.png?fit=1024%2C538&amp;ssl=1\" data-original=\"https:\/\/www.linerroom.cn\/wp-content\/uploads\/2025\/11\/image-32-1024x538.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-283\"  sizes=\"(max-width: 1000px) 100vw, 1000px\" \/><\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-pale-cyan-blue-color\">\u5b66\u4e60\u8def\u7ebf\uff08\u81ea\uff09<\/mark><\/strong><\/h2>\n\n\n\n<div style=\"margin-bottom: 20px;\" class=\"wp-block-argon-todolist\"><div class=\"shortcode-todo custom-control custom-checkbox\"><input class=\"custom-control-input\" type=\"checkbox\" checked\/><label class=\"custom-control-label\"><span>node.js\u73af\u5883\u642d\u5efa<\/span><\/label><\/div><div class=\"shortcode-todo custom-control custom-checkbox\"><input class=\"custom-control-input\" type=\"checkbox\" checked\/><label class=\"custom-control-label\"><span>express\u6846\u67b6\u4f7f\u7528<\/span><\/label><\/div><div class=\"shortcode-todo custom-control custom-checkbox\"><input class=\"custom-control-input\" type=\"checkbox\" checked\/><label class=\"custom-control-label\"><span>\u7f16\u5199\u9879\u76ee\u8def\u7531<\/span><\/label><\/div><div class=\"shortcode-todo custom-control custom-checkbox\"><input class=\"custom-control-input\" type=\"checkbox\" checked\/><label class=\"custom-control-label\"><span>\u5b8c\u6210\u589e\u5220\u6539\u67e5\u57fa\u7840\u529f\u80fd<\/span><\/label><\/div><div class=\"shortcode-todo custom-control custom-checkbox\"><input class=\"custom-control-input\" type=\"checkbox\"\/><label class=\"custom-control-label\"><span>\u5bf9\u6570\u636e\u5e93\uff0capi\u8bbe\u8ba1<\/span><\/label><\/div><div class=\"shortcode-todo custom-control custom-checkbox\"><input class=\"custom-control-input\" type=\"checkbox\"\/><label class=\"custom-control-label\"><span>\u5b8c\u6210\u9ad8\u7ea7api\uff0c\u4e0a\u4f20\u6587\u4ef6\uff0c\u89c6\u9891\u4f20\u8f93<\/span><\/label><\/div><div class=\"shortcode-todo custom-control custom-checkbox\"><input class=\"custom-control-input\" type=\"checkbox\"\/><label class=\"custom-control-label\"><span>api\u4f18\u5316<\/span><\/label><\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">\u540e\u7aef\u6280\u672f\u7edf\u8ba1\uff1a<\/h2>\n\n\n\n<div class=\"wp-block-argon-admonition admonition shadow-sm\" style=\"border-left-color:#7889e8\"><div class=\"admonition-body\">\u9879\u76ee\u914d\u7f6e\u6587\u4ef6\uff1a<br>{<br>\u00a0 &#8220;dependencies&#8221;: {<br>\u00a0 \u00a0 &#8220;body-parser&#8221;: &#8220;^2.2.0&#8221;,<br>\u00a0 \u00a0 &#8220;cookie-parser&#8221;: &#8220;^1.4.7&#8221;,<br>\u00a0 \u00a0 &#8220;express&#8221;: &#8220;^5.1.0&#8221;,<br>\u00a0 \u00a0 &#8220;multer&#8221;: &#8220;^2.0.2&#8221;,<br>\u00a0 \u00a0 &#8220;mongoose&#8221;: &#8220;^7.5.0&#8221;,<br>\u00a0 \u00a0 &#8220;jsonwebtoken&#8221;: &#8220;^9.0.0&#8221;<br>\u00a0 }<br>}<br>\u4f7f\u7528\u4ee5\u4e0a\u9700\u8981\u5b89\u88c5\u4f9d\u8d56\uff1anpm install<\/div><\/div>\n\n\n\n<div class=\"wp-block-argon-admonition admonition shadow-sm\" style=\"border-left-color:#7889e8\"><div class=\"admonition-body\"><code>express<\/code>\u00a0\u642d\u5efa\u670d\u52a1\u5668\u548c\u8def\u7531\uff1b<br><code>mongoose<\/code>\u00a0\u64cd\u4f5c MongoDB \u6570\u636e\u5e93\uff1b<br><code>jsonwebtoken<\/code>\u00a0\u5904\u7406\u8eab\u4efd\u8ba4\u8bc1\uff1b<br><code>body-parser<\/code>\/<code>cookie-parser<\/code>\u00a0\u89e3\u6790\u8bf7\u6c42\u6570\u636e\uff1b<br><code>multer<\/code>\u00a0\u5904\u7406\u6587\u4ef6\u4e0a\u4f20\u3002<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">\u6700\u7b80\u5355\u7ed3\u6784<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/express_demo.js \u6587\u4ef6\nvar express = require('express');\nvar app = express();\n \napp.get('\/', function (req, res) {\n   res.send('Hello World');\n})\n \nvar server = app.listen(8081, function () {\n \n  var host = server.address().address\n  var port = server.address().port\n \n  console.log(\"\u5e94\u7528\u5b9e\u4f8b\uff0c\u8bbf\u95ee\u5730\u5740\u4e3a http:\/\/%s:%s\", host, port)\n \n})<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u8bf7\u6c42\u548c\u54cd\u5e94\u5bf9\u8c61<\/h2>\n\n\n\n<p>Express \u5e94\u7528\u4f7f\u7528\u56de\u8c03\u51fd\u6570\u7684\u53c2\u6570\uff1a&nbsp;<strong>request<\/strong>&nbsp;\u548c&nbsp;<strong>response<\/strong>&nbsp;\u5bf9\u8c61\u6765\u5904\u7406\u8bf7\u6c42\u548c\u54cd\u5e94\u7684\u6570\u636e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>app.get('\/', function (req, res) {\n   \/\/ --\n})\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>request \u548c response \u5bf9\u8c61\u7684\u5177\u4f53\u4ecb\u7ecd\uff1a\n\nRequest \u5bf9\u8c61 - request \u5bf9\u8c61\u8868\u793a HTTP \u8bf7\u6c42\uff0c\u5305\u542b\u4e86\u8bf7\u6c42\u67e5\u8be2\u5b57\u7b26\u4e32\uff0c\u53c2\u6570\uff0c\u5185\u5bb9\uff0cHTTP \u5934\u90e8\u7b49\u5c5e\u6027\u3002\u5e38\u89c1\u5c5e\u6027\u6709\uff1a\n\nreq.app\uff1a\u5f53callback\u4e3a\u5916\u90e8\u6587\u4ef6\u65f6\uff0c\u7528req.app\u8bbf\u95eeexpress\u7684\u5b9e\u4f8b\nreq.baseUrl\uff1a\u83b7\u53d6\u8def\u7531\u5f53\u524d\u5b89\u88c5\u7684URL\u8def\u5f84\nreq.body \/ req.cookies\uff1a\u83b7\u5f97\u300c\u8bf7\u6c42\u4e3b\u4f53\u300d\/ Cookies\nreq.fresh \/ req.stale\uff1a\u5224\u65ad\u8bf7\u6c42\u662f\u5426\u8fd8\u300c\u65b0\u9c9c\u300d\nreq.hostname \/ req.ip\uff1a\u83b7\u53d6\u4e3b\u673a\u540d\u548cIP\u5730\u5740\nreq.originalUrl\uff1a\u83b7\u53d6\u539f\u59cb\u8bf7\u6c42URL\nreq.params\uff1a\u83b7\u53d6\u8def\u7531\u7684parameters\nreq.path\uff1a\u83b7\u53d6\u8bf7\u6c42\u8def\u5f84\nreq.protocol\uff1a\u83b7\u53d6\u534f\u8bae\u7c7b\u578b\nreq.query\uff1a\u83b7\u53d6URL\u7684\u67e5\u8be2\u53c2\u6570\u4e32\nreq.route\uff1a\u83b7\u53d6\u5f53\u524d\u5339\u914d\u7684\u8def\u7531\nreq.subdomains\uff1a\u83b7\u53d6\u5b50\u57df\u540d\nreq.accepts()\uff1a\u68c0\u67e5\u53ef\u63a5\u53d7\u7684\u8bf7\u6c42\u7684\u6587\u6863\u7c7b\u578b\nreq.acceptsCharsets \/ req.acceptsEncodings \/ req.acceptsLanguages\uff1a\u8fd4\u56de\u6307\u5b9a\u5b57\u7b26\u96c6\u7684\u7b2c\u4e00\u4e2a\u53ef\u63a5\u53d7\u5b57\u7b26\u7f16\u7801\nreq.get()\uff1a\u83b7\u53d6\u6307\u5b9a\u7684HTTP\u8bf7\u6c42\u5934\nreq.is()\uff1a\u5224\u65ad\u8bf7\u6c42\u5934Content-Type\u7684MIME\u7c7b\u578b\nResponse \u5bf9\u8c61 - response \u5bf9\u8c61\u8868\u793a HTTP \u54cd\u5e94\uff0c\u5373\u5728\u63a5\u6536\u5230\u8bf7\u6c42\u65f6\u5411\u5ba2\u6237\u7aef\u53d1\u9001\u7684 HTTP \u54cd\u5e94\u6570\u636e\u3002\u5e38\u89c1\u5c5e\u6027\u6709\uff1a\n\nres.app\uff1a\u540creq.app\u4e00\u6837\nres.append()\uff1a\u8ffd\u52a0\u6307\u5b9aHTTP\u5934\nres.set()\u5728res.append()\u540e\u5c06\u91cd\u7f6e\u4e4b\u524d\u8bbe\u7f6e\u7684\u5934\nres.cookie(name\uff0cvalue &#91;\uff0coption])\uff1a\u8bbe\u7f6eCookie\nopition: domain \/ expires \/ httpOnly \/ maxAge \/ path \/ secure \/ signed\nres.clearCookie()\uff1a\u6e05\u9664Cookie\nres.download()\uff1a\u4f20\u9001\u6307\u5b9a\u8def\u5f84\u7684\u6587\u4ef6\nres.get()\uff1a\u8fd4\u56de\u6307\u5b9a\u7684HTTP\u5934\nres.json()\uff1a\u4f20\u9001JSON\u54cd\u5e94\nres.jsonp()\uff1a\u4f20\u9001JSONP\u54cd\u5e94\nres.location()\uff1a\u53ea\u8bbe\u7f6e\u54cd\u5e94\u7684Location HTTP\u5934\uff0c\u4e0d\u8bbe\u7f6e\u72b6\u6001\u7801\u6216\u8005close response\nres.redirect()\uff1a\u8bbe\u7f6e\u54cd\u5e94\u7684Location HTTP\u5934\uff0c\u5e76\u4e14\u8bbe\u7f6e\u72b6\u6001\u7801302\nres.render(view,&#91;locals],callback)\uff1a\u6e32\u67d3\u4e00\u4e2aview\uff0c\u540c\u65f6\u5411callback\u4f20\u9012\u6e32\u67d3\u540e\u7684\u5b57\u7b26\u4e32\uff0c\u5982\u679c\u5728\u6e32\u67d3\u8fc7\u7a0b\u4e2d\u6709\u9519\u8bef\u53d1\u751fnext(err)\u5c06\u4f1a\u88ab\u81ea\u52a8\u8c03\u7528\u3002callback\u5c06\u4f1a\u88ab\u4f20\u5165\u4e00\u4e2a\u53ef\u80fd\u53d1\u751f\u7684\u9519\u8bef\u4ee5\u53ca\u6e32\u67d3\u540e\u7684\u9875\u9762\uff0c\u8fd9\u6837\u5c31\u4e0d\u4f1a\u81ea\u52a8\u8f93\u51fa\u4e86\u3002\nres.send()\uff1a\u4f20\u9001HTTP\u54cd\u5e94\nres.sendFile(path &#91;\uff0coptions] &#91;\uff0cfn])\uff1a\u4f20\u9001\u6307\u5b9a\u8def\u5f84\u7684\u6587\u4ef6 -\u4f1a\u81ea\u52a8\u6839\u636e\u6587\u4ef6extension\u8bbe\u5b9aContent-Type\nres.set()\uff1a\u8bbe\u7f6eHTTP\u5934\uff0c\u4f20\u5165object\u53ef\u4ee5\u4e00\u6b21\u8bbe\u7f6e\u591a\u4e2a\u5934\nres.status()\uff1a\u8bbe\u7f6eHTTP\u72b6\u6001\u7801\nres.type()\uff1a\u8bbe\u7f6eContent-Type\u7684MIME\u7c7b\u578b<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9e\u4f8b-\u8fde\u63a5mongoDB\u6570\u636e\u5e93\uff1a<\/h2>\n\n\n\n<p>\u5b9a\u4e49\u8fde\u63a5\u6a21\u5757-\u6570\u636e\u5e93\u542f\u52a8\u6210\u529f\u518d\u542f\u52a8\u670d\u52a1-api\u6587\u6863\u53ea\u7528\u83b7\u53d6\u8fde\u63a5\u5bf9\u8c61\uff08\u4ee5\u4e0b\u4ec5\u7ed9\u4e86\u5173\u952e\u4ee3\u7801\uff09<\/p>\n\n\n\n<p>\u4ee3\u7801\u4ec5\u4f9b\u53c2\u8003-\u8fd8\u9700\u8981\u81ea\u884c\u7f16\u5199\u8def\u7531\u914d\u7f6e<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u6570\u636e\u5e93\u8fde\u63a5\u683c\u5f0f<\/h2>\n\n\n\n<div class=\"wp-block-argon-admonition admonition shadow-sm\" style=\"border-left-color:#7889e8\"><div class=\"admonition-body\">mongodb:\/\/\u7528\u6237\u540d:\u5bc6\u7801@IP\u5730\u5740:27017\/\u5e93\u540d<\/div><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">\u542f\u52a8\u6587\u4ef6server.js<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u670d\u52a1\u5668\u542f\u52a8\u5165\u53e3\nconst app = require('.\/app');\nconst config = require('.\/config');\nconst db = require('.\/config\/db');\n\n\/\/ \u83b7\u53d6\u7aef\u53e3\u914d\u7f6e\nconst PORT = config.port || 4411;\n\n\/\/ \u5148\u8fde\u63a5\u6570\u636e\u5e93\uff0c\u8fde\u63a5\u6210\u529f\u540e\u518d\u542f\u52a8\u670d\u52a1\u5668\ndb.connect()\n&nbsp; &nbsp; .then(() =&gt; {\n&nbsp; &nbsp; &nbsp; &nbsp; console.log('MongoDB \u5df2\u8fde\u63a5');\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/ \u542f\u52a8\u670d\u52a1\u5668\n&nbsp; &nbsp; &nbsp; &nbsp; const server = app.listen(PORT, () =&gt; {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(`\u670d\u52a1\u5668\u8fd0\u884c\u5728 http:\/\/localhost:${PORT}`);\n&nbsp; &nbsp; &nbsp; &nbsp; });\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/ \u6dfb\u52a0\u9519\u8bef\u76d1\u542c\u5668\uff0c\u5904\u7406\u670d\u52a1\u5668\u542f\u52a8\u9519\u8bef\n&nbsp; &nbsp; &nbsp; &nbsp; server.on('error', function(err) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log('\u670d\u52a1\u5668\u542f\u52a8\u9519\u8bef:');\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (err.code === 'EADDRINUSE') {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(' - \u9519\u8bef\u4ee3\u7801: EADDRINUSE');\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(` - \u9519\u8bef\u63cf\u8ff0: \u7aef\u53e3${PORT}\u5df2\u88ab\u5360\u7528`);\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(' - \u89e3\u51b3\u65b9\u6cd5: \u8bf7\u5173\u95ed\u5360\u7528\u8be5\u7aef\u53e3\u7684\u5e94\u7528\u6216\u4fee\u6539\u670d\u52a1\u5668\u7aef\u53e3');\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(' - \u9519\u8bef\u4ee3\u7801:', err.code);\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(' - \u9519\u8bef\u4fe1\u606f:', err.message);\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; process.exit(1);\n&nbsp; &nbsp; &nbsp; &nbsp; });\n&nbsp; &nbsp; })\n&nbsp; &nbsp; .catch(err =&gt; {\n&nbsp; &nbsp; &nbsp; &nbsp; console.error('\u65e0\u6cd5\u8fde\u63a5\u5230 MongoDB:', err);\n&nbsp; &nbsp; &nbsp; &nbsp; process.exit(1);\n&nbsp; &nbsp; });\n\n\/\/ \u5904\u7406\u672a\u6355\u83b7\u7684\u5f02\u5e38\nprocess.on('uncaughtException', (error) =&gt; {\n&nbsp; &nbsp; console.error('\u672a\u6355\u83b7\u7684\u5f02\u5e38:', error);\n&nbsp; &nbsp; process.exit(1);\n});\n\n\/\/ \u5904\u7406Promise rejection\nprocess.on('unhandledRejection', (reason, promise) =&gt; {\n&nbsp; &nbsp; console.error('\u672a\u5904\u7406\u7684Promise rejection:', reason);\n});\n\nmodule.exports = null; \/\/ server \u5728\u6210\u529f\u8fde\u63a5\u540e\u624d\u4f1a\u521b\u5efa\u5e76\u76d1\u542c<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">DB\u6570\u636e\u5e93\u5b9a\u4e49db.js<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ MongoDB \u8fde\u63a5\u6a21\u5757\nconst mongoose = require('mongoose');\nconst config = require('.\/index');\n\nasync function connect() {\n&nbsp; const uri = process.env.MONGO_URI || config.mongoUri;\n&nbsp; if (!uri) {\n&nbsp; &nbsp; throw new Error('MongoDB URI \u672a\u914d\u7f6e\uff0c\u8bf7\u5728 config\/index.js \u4e2d\u8bbe\u7f6e mongoUri \u6216\u901a\u8fc7\u73af\u5883\u53d8\u91cf MONGO_URI \u4f20\u5165\u3002');\n&nbsp; }\n\n&nbsp; try {\n&nbsp; &nbsp; \/\/ mongoose 7+ \u5df2\u4e0d\u518d\u9700\u8981 useNewUrlParser\/useUnifiedTopology\uff0c\u4f46\u4fdd\u7559\u4e5f\u4e0d\u4f1a\u62a5\u9519\n&nbsp; &nbsp; await mongoose.connect(uri, {\n&nbsp; &nbsp; &nbsp; \/\/ \u8fd9\u4e9b\u9009\u9879\u5728\u65b0\u7248\u672c\u4e2d\u9ed8\u8ba4\u542f\u7528\uff0c\u4f46\u663e\u5f0f\u58f0\u660e\u4ee5\u517c\u5bb9\u8001\u73af\u5883\n&nbsp; &nbsp; &nbsp; useNewUrlParser: true,\n&nbsp; &nbsp; &nbsp; useUnifiedTopology: true\n&nbsp; &nbsp; });\n&nbsp; &nbsp; console.log('MongoDB \u5df2\u8fde\u63a5');\n&nbsp; } catch (err) {\n&nbsp; &nbsp; console.error('\u65e0\u6cd5\u8fde\u63a5\u5230 MongoDB:', err);\n&nbsp; &nbsp; throw err;\n&nbsp; }\n}\n\nmodule.exports = {\n&nbsp; connect,\n&nbsp; mongoose\n};<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u62ff\u5bf9\u8c61\u5199api-dbcz.js<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>const express = require('express');\nconst router = express.Router();\nconst { ObjectId } = require('mongodb');\n\/\/ \u4f7f\u7528\u5df2\u5b58\u5728\u7684 mongoose \u8fde\u63a5\uff08\u7531 db.js \u5728 server \u542f\u52a8\u65f6\u5efa\u7acb\uff09\nconst { mongoose } = require('..\/config\/db');\n\nfunction getColl() {\n&nbsp; const conn = mongoose.connection;\n&nbsp; if (!conn || !conn.db) throw new Error('\u6570\u636e\u5e93\u5c1a\u672a\u8fde\u63a5');\n&nbsp; return conn.db.collection('items');\n}\n\n\/\/ \u67e5\nrouter.get('\/db\/items', async (req, res) =&gt; {\n&nbsp; try {\n\n\n&nbsp; &nbsp; const coll = getColl();\n&nbsp; &nbsp; const docs = await coll.find({}).toArray();\n\n&nbsp; &nbsp; res.json(docs);\n\n\n&nbsp; } catch (err) {\n&nbsp; &nbsp; console.error(err);\n&nbsp; &nbsp; res.status(500).json({ error: '\u83b7\u53d6\u5217\u8868\u5931\u8d25' });\n&nbsp; }\n});\n\n\n\/\/ \u6dfb\u52a0\nrouter.post('\/db\/items', async (req, res) =&gt; {\n&nbsp; try {\n&nbsp; &nbsp; const data = req.body || {};\n&nbsp; &nbsp; const coll = getColl();\n&nbsp; &nbsp; const r = await coll.insertOne(data);\n\n&nbsp; &nbsp; res.status(201).json({text:'\u6dfb\u52a0\u6210\u529f'});\n\n&nbsp; } catch (err) {\n&nbsp; &nbsp; console.error(err);\n&nbsp; &nbsp; res.status(500).json({ error: '\u521b\u5efa\u5931\u8d25' });\n&nbsp; }\n});\n\n\n\nmodule.exports = router;\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading has-text-align-center\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u7b80\u5355\u9884\u89c8<\/mark><\/h2>\n\n\n\n<div class=\"argon-tabpanel\" class=\"wp-block-argon-tabpanel\"><div class=\"tabpanel-header nav-wrapper\"><ul class=\"nav nav-pills nav-fill\" role=\"tablist\"><li class=\"nav-item\"><a class=\"nav-link \" id=\"tabpanel-AhyXbqTN-0-tab\" href=\"#tabpanel-AhyXbqTN-0\" data-toggle=\"tab\" role=\"tab\" aria-controls=\"#tabpanel-AhyXbqTN-0\" aria-selected=\"false\">\u542f\u52a8\u6587\u4ef6server.js<\/a><\/li><li class=\"nav-item\"><a class=\"nav-link \" id=\"tabpanel-AhyXbqTN-1-tab\" href=\"#tabpanel-AhyXbqTN-1\" data-toggle=\"tab\" role=\"tab\" aria-controls=\"#tabpanel-AhyXbqTN-1\" aria-selected=\"false\">DB\u6570\u636e\u5e93\u5b9a\u4e49db.js<\/a><\/li><li class=\"nav-item\"><a class=\"nav-link active\" id=\"tabpanel-AhyXbqTN-2-tab\" href=\"#tabpanel-AhyXbqTN-2\" data-toggle=\"tab\" role=\"tab\" aria-controls=\"#tabpanel-AhyXbqTN-2\" aria-selected=\"false\">\u62ff\u5bf9\u8c61\u5199api-dbcz.js<\/a><\/li><\/ul><\/div><div class=\"tabpanel-body card card-body shadow-sm\"><div class=\"tab-content\"><div class=\"tab-pane  fade \" id=\"tabpanel-AhyXbqTN-0\" role=\"tabpanel\" aria-labelledby=\"tabpanel-AhyXbqTN-0-tab\">\/\/ \u670d\u52a1\u5668\u542f\u52a8\u5165\u53e3<br>const app = require(&#8216;.\/app&#8217;);<br>const config = require(&#8216;.\/config&#8217;);<br>const db = require(&#8216;.\/config\/db&#8217;);<br><br>\/\/ \u83b7\u53d6\u7aef\u53e3\u914d\u7f6e<br>const PORT = config.port || 4411;<br><br>\/\/ \u5148\u8fde\u63a5\u6570\u636e\u5e93\uff0c\u8fde\u63a5\u6210\u529f\u540e\u518d\u542f\u52a8\u670d\u52a1\u5668<br>db.connect()<br>\u00a0 \u00a0 .then(() => {<br>\u00a0 \u00a0 \u00a0 \u00a0 console.log(&#8216;MongoDB \u5df2\u8fde\u63a5&#8217;);<br>\u00a0 \u00a0 \u00a0 \u00a0 \/\/ \u542f\u52a8\u670d\u52a1\u5668<br>\u00a0 \u00a0 \u00a0 \u00a0 const server = app.listen(PORT, () => {<br>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 console.log(`\u670d\u52a1\u5668\u8fd0\u884c\u5728 http:\/\/localhost:${PORT}`);<br>\u00a0 \u00a0 \u00a0 \u00a0 });<br>\u00a0 \u00a0 \u00a0 \u00a0 \/\/ \u6dfb\u52a0\u9519\u8bef\u76d1\u542c\u5668\uff0c\u5904\u7406\u670d\u52a1\u5668\u542f\u52a8\u9519\u8bef<br>\u00a0 \u00a0 \u00a0 \u00a0 server.on(&#8216;error&#8217;, function(err) {<br>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 console.log(&#8216;\u670d\u52a1\u5668\u542f\u52a8\u9519\u8bef:&#8217;);<br>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 if (err.code === &#8216;EADDRINUSE&#8217;) {<br>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 console.log(&#8216; &#8211; \u9519\u8bef\u4ee3\u7801: EADDRINUSE&#8217;);<br>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 console.log(` &#8211; \u9519\u8bef\u63cf\u8ff0: \u7aef\u53e3${PORT}\u5df2\u88ab\u5360\u7528`);<br>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 console.log(&#8216; &#8211; \u89e3\u51b3\u65b9\u6cd5: \u8bf7\u5173\u95ed\u5360\u7528\u8be5\u7aef\u53e3\u7684\u5e94\u7528\u6216\u4fee\u6539\u670d\u52a1\u5668\u7aef\u53e3&#8217;);<br>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 } else {<br>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 console.log(&#8216; &#8211; \u9519\u8bef\u4ee3\u7801:&#8217;, err.code);<br>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 console.log(&#8216; &#8211; \u9519\u8bef\u4fe1\u606f:&#8217;, err.message);<br>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<br>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 process.exit(1);<br>\u00a0 \u00a0 \u00a0 \u00a0 });<br>\u00a0 \u00a0 })<br>\u00a0 \u00a0 .catch(err => {<br>\u00a0 \u00a0 \u00a0 \u00a0 console.error(&#8216;\u65e0\u6cd5\u8fde\u63a5\u5230 MongoDB:&#8217;, err);<br>\u00a0 \u00a0 \u00a0 \u00a0 process.exit(1);<br>\u00a0 \u00a0 });<br><br>\/\/ \u5904\u7406\u672a\u6355\u83b7\u7684\u5f02\u5e38<br>process.on(&#8216;uncaughtException&#8217;, (error) => {<br>\u00a0 \u00a0 console.error(&#8216;\u672a\u6355\u83b7\u7684\u5f02\u5e38:&#8217;, error);<br>\u00a0 \u00a0 process.exit(1);<br>});<br><br>\/\/ \u5904\u7406Promise rejection<br>process.on(&#8216;unhandledRejection&#8217;, (reason, promise) => {<br>\u00a0 \u00a0 console.error(&#8216;\u672a\u5904\u7406\u7684Promise rejection:&#8217;, reason);<br>});<br><br>module.exports = null; \/\/ server \u5728\u6210\u529f\u8fde\u63a5\u540e\u624d\u4f1a\u521b\u5efa\u5e76\u76d1\u542c<\/div><div class=\"tab-pane  fade \" id=\"tabpanel-AhyXbqTN-1\" role=\"tabpanel\" aria-labelledby=\"tabpanel-AhyXbqTN-1-tab\">\/\/ MongoDB \u8fde\u63a5\u6a21\u5757<br>const mongoose = require(&#8216;mongoose&#8217;);<br>const config = require(&#8216;.\/index&#8217;);<br><br>async function connect() {<br>\u00a0 const uri = process.env.MONGO_URI || config.mongoUri;<br>\u00a0 if (!uri) {<br>\u00a0 \u00a0 throw new Error(&#8216;MongoDB URI \u672a\u914d\u7f6e\uff0c\u8bf7\u5728 config\/index.js \u4e2d\u8bbe\u7f6e mongoUri \u6216\u901a\u8fc7\u73af\u5883\u53d8\u91cf MONGO_URI \u4f20\u5165\u3002&#8217;);<br>\u00a0 }<br><br>\u00a0 try {<br>\u00a0 \u00a0 \/\/ mongoose 7+ \u5df2\u4e0d\u518d\u9700\u8981 useNewUrlParser\/useUnifiedTopology\uff0c\u4f46\u4fdd\u7559\u4e5f\u4e0d\u4f1a\u62a5\u9519<br>\u00a0 \u00a0 await mongoose.connect(uri, {<br>\u00a0 \u00a0 \u00a0 \/\/ \u8fd9\u4e9b\u9009\u9879\u5728\u65b0\u7248\u672c\u4e2d\u9ed8\u8ba4\u542f\u7528\uff0c\u4f46\u663e\u5f0f\u58f0\u660e\u4ee5\u517c\u5bb9\u8001\u73af\u5883<br>\u00a0 \u00a0 \u00a0 useNewUrlParser: true,<br>\u00a0 \u00a0 \u00a0 useUnifiedTopology: true<br>\u00a0 \u00a0 });<br>\u00a0 \u00a0 console.log(&#8216;MongoDB \u5df2\u8fde\u63a5&#8217;);<br>\u00a0 } catch (err) {<br>\u00a0 \u00a0 console.error(&#8216;\u65e0\u6cd5\u8fde\u63a5\u5230 MongoDB:&#8217;, err);<br>\u00a0 \u00a0 throw err;<br>\u00a0 }<br>}<br><br>module.exports = {<br>\u00a0 connect,<br>\u00a0 mongoose<br>};<\/div><div class=\"tab-pane  fade  active show\" id=\"tabpanel-AhyXbqTN-2\" role=\"tabpanel\" aria-labelledby=\"tabpanel-AhyXbqTN-2-tab\">const express = require(&#8216;express&#8217;);<br>const router = express.Router();<br>const { ObjectId } = require(&#8216;mongodb&#8217;);<br>\/\/ \u4f7f\u7528\u5df2\u5b58\u5728\u7684 mongoose \u8fde\u63a5\uff08\u7531 db.js \u5728 server \u542f\u52a8\u65f6\u5efa\u7acb\uff09<br>const { mongoose } = require(&#8216;..\/config\/db&#8217;);<br><br>function getColl() {<br>\u00a0 const conn = mongoose.connection;<br>\u00a0 if (!conn || !conn.db) throw new Error(&#8216;\u6570\u636e\u5e93\u5c1a\u672a\u8fde\u63a5&#8217;);<br>\u00a0 return conn.db.collection(&#8216;items&#8217;);<br>}<br><br>\/\/ \u67e5<br>router.get(&#8216;\/db\/items&#8217;, async (req, res) => {<br>\u00a0 try {<br><br><br>\u00a0 \u00a0 const coll = getColl();<br>\u00a0 \u00a0 const docs = await coll.find({}).toArray();<br><br>\u00a0 \u00a0 res.json(docs);<br><br><br>\u00a0 } catch (err) {<br>\u00a0 \u00a0 console.error(err);<br>\u00a0 \u00a0 res.status(500).json({ error: &#8216;\u83b7\u53d6\u5217\u8868\u5931\u8d25&#8217; });<br>\u00a0 }<br>});<br><br><br>\/\/ \u6dfb\u52a0<br>router.post(&#8216;\/db\/items&#8217;, async (req, res) => {<br>\u00a0 try {<br>\u00a0 \u00a0 const data = req.body || {};<br>\u00a0 \u00a0 const coll = getColl();<br>\u00a0 \u00a0 const r = await coll.insertOne(data);<br><br>\u00a0 \u00a0 res.status(201).json({text:&#8217;\u6dfb\u52a0\u6210\u529f&#8217;});<br><br>\u00a0 } catch (err) {<br>\u00a0 \u00a0 console.error(err);<br>\u00a0 \u00a0 res.status(500).json({ error: &#8216;\u521b\u5efa\u5931\u8d25&#8217; });<br>\u00a0 }<br>});<br><br><br><br>module.exports = router;<br><\/div><\/div><\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b66\u4e60\u8def\u7ebf\uff08\u81ea\uff09 \u540e\u7aef\u6280\u672f\u7edf\u8ba1\uff1a \u6700\u7b80\u5355\u7ed3\u6784 \u8bf7\u6c42\u548c\u54cd\u5e94\u5bf9\u8c61 Express \u5e94\u7528\u4f7f\u7528\u56de\u8c03\u51fd\u6570\u7684\u53c2\u6570\uff1a&#038;nbsp [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[17],"tags":[],"class_list":["post-282","post","type-post","status-publish","format-standard","hentry","category-node"],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=\/wp\/v2\/posts\/282","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=282"}],"version-history":[{"count":12,"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=\/wp\/v2\/posts\/282\/revisions"}],"predecessor-version":[{"id":331,"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=\/wp\/v2\/posts\/282\/revisions\/331"}],"wp:attachment":[{"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=282"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.linerroom.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}