{"id":291,"date":"2026-10-21T10:30:06","date_gmt":"2026-10-21T02:30:06","guid":{"rendered":"https:\/\/yykcj.com\/?p=291"},"modified":"2026-10-21T10:30:06","modified_gmt":"2026-10-21T02:30:06","slug":"node-js-%e9%94%99%e8%af%af%e5%a4%84%e7%90%86%e6%9c%80%e4%bd%b3%e5%ae%9e%e8%b7%b5%ef%bc%9a%e4%bb%8e%e5%9b%9e%e8%b0%83%e5%9c%b0%e7%8b%b1%e5%88%b0%e4%bc%98%e9%9b%85%e9%99%8d%e7%ba%a7","status":"publish","type":"post","link":"https:\/\/yykcj.com\/?p=291","title":{"rendered":"Node.js \u9519\u8bef\u5904\u7406\u6700\u4f73\u5b9e\u8df5\uff1a\u4ece\u56de\u8c03\u5730\u72f1\u5230\u4f18\u96c5\u964d\u7ea7"},"content":{"rendered":"<h2>\u5f15\u8a00<\/h2>\n<p>\u5728 Node.js \u5f00\u53d1\u4e2d\uff0c\u9519\u8bef\u5904\u7406\u662f\u6784\u5efa\u53ef\u9760\u5e94\u7528\u7684\u5173\u952e\uff0c\u5374\u5e38\u88ab\u5ffd\u89c6\u3002\u8bb8\u591a\u5f00\u53d1\u8005\u4e60\u60ef\u7528 <code>try...catch<\/code> \u5305\u88f9\u4e00\u5207\uff0c\u6216\u5ffd\u7565\u56de\u8c03\u4e2d\u7684\u9519\u8bef\u53c2\u6570\uff0c\u5bfc\u81f4\u5e94\u7528\u5d29\u6e83\u3001\u8d44\u6e90\u6cc4\u6f0f\u6216\u96be\u4ee5\u8c03\u8bd5\u7684 Bug\u3002\u672c\u6587\u5c06\u5e26\u4f60\u4ece\u57fa\u7840\u5230\u8fdb\u9636\uff0c\u7cfb\u7edf\u638c\u63e1 Node.js \u9519\u8bef\u5904\u7406\u7684\u6700\u4f73\u5b9e\u8df5\uff0c\u5e76\u7ed9\u51fa\u53ef\u76f4\u63a5\u8fd0\u884c\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<h2>1. \u9519\u8bef\u5904\u7406\u7684\u57fa\u7840\uff1a\u56de\u8c03\u3001Promise \u4e0e async\/await<\/h2>\n<p>Node.js \u7684\u9519\u8bef\u5904\u7406\u6a21\u5f0f\u7ecf\u5386\u4e86\u4ece\u56de\u8c03\u5230 Promise \u518d\u5230 async\/await \u7684\u6f14\u8fdb\u3002\u7406\u89e3\u6bcf\u79cd\u6a21\u5f0f\u7684\u6b63\u786e\u5904\u7406\u65b9\u5f0f\u662f\u57fa\u7840\u3002<\/p>\n<h3>1.1 \u56de\u8c03\u6a21\u5f0f\uff1a\u9519\u8bef\u4f18\u5148\uff08Error-First\uff09<\/h3>\n<p>Node.js \u7684\u6838\u5fc3 API \u5927\u591a\u9075\u5faa\u9519\u8bef\u4f18\u5148\u56de\u8c03\u7ea6\u5b9a\uff1a\u56de\u8c03\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u9519\u8bef\u5bf9\u8c61\uff0c\u7b2c\u4e8c\u4e2a\u662f\u7ed3\u679c\u3002<\/p>\n<pre><code class=\"language-javascript\">\nconst fs = require(&#x27;fs&#x27;);\n\nfs.readFile(&#x27;\/path\/to\/file&#x27;, (err, data) =&gt; {\n  if (err) {\n    \/\/ \u5904\u7406\u9519\u8bef\n    console.error(&#x27;\u8bfb\u53d6\u6587\u4ef6\u5931\u8d25:&#x27;, err);\n    return;\n  }\n  console.log(data.toString());\n});\n<\/code><\/pre>\n<p><strong>\u6ce8\u610f<\/strong>\uff1a\u6c38\u8fdc\u4e0d\u8981\u5ffd\u7565 <code>err<\/code> \u53c2\u6570\uff0c\u5373\u4f7f\u4f60\u8ba4\u4e3a\u4e0d\u4f1a\u51fa\u9519\u3002<\/p>\n<h3>1.2 Promise\uff1a\u4f7f\u7528 <code>.catch()<\/code> \u6216 <code>try...catch<\/code><\/h3>\n<p>Promise \u94fe\u5f0f\u8c03\u7528\u4e2d\uff0c\u52a1\u5fc5\u5728\u94fe\u5c3e\u6dfb\u52a0 <code>.catch()<\/code>\uff1a<\/p>\n<pre><code class=\"language-javascript\">\nconst fs = require(&#x27;fs&#x27;).promises;\n\nfs.readFile(&#x27;\/path\/to\/file&#x27;)\n  .then(data =&gt; console.log(data.toString()))\n  .catch(err =&gt; console.error(&#x27;\u8bfb\u53d6\u6587\u4ef6\u5931\u8d25:&#x27;, err));\n<\/code><\/pre>\n<h3>1.3 async\/await\uff1a\u540c\u6b65\u98ce\u683c\u7684\u9519\u8bef\u5904\u7406<\/h3>\n<p>async\/await \u8ba9\u5f02\u6b65\u4ee3\u7801\u770b\u8d77\u6765\u50cf\u540c\u6b65\uff0c\u53ef\u4ee5\u7528 <code>try...catch<\/code> \u6355\u83b7\u9519\u8bef\uff1a<\/p>\n<pre><code class=\"language-javascript\">\nconst fs = require(&#x27;fs&#x27;).promises;\n\nasync function readFile() {\n  try {\n    const data = await fs.readFile(&#x27;\/path\/to\/file&#x27;);\n    console.log(data.toString());\n  } catch (err) {\n    console.error(&#x27;\u8bfb\u53d6\u6587\u4ef6\u5931\u8d25:&#x27;, err);\n  }\n}\n<\/code><\/pre>\n<p><strong>\ud83d\udca1 \u63d0\u793a<\/strong>\uff1a\u5728 async \u51fd\u6570\u4e2d\uff0c\u672a\u6355\u83b7\u7684 Promise \u62d2\u7edd\u4f1a\u5bfc\u81f4 unhandledRejection\uff0c\u5e94\u59cb\u7ec8\u4f7f\u7528 try&#8230;catch \u6216 .catch()\u3002<\/p>\n<h2>2. \u521b\u5efa\u81ea\u5b9a\u4e49\u9519\u8bef\u7c7b<\/h2>\n<p>\u5185\u7f6e\u7684 Error \u7c7b\u4fe1\u606f\u6709\u9650\u3002\u901a\u8fc7\u7ee7\u627f Error \u521b\u5efa\u81ea\u5b9a\u4e49\u9519\u8bef\u7c7b\uff0c\u53ef\u4ee5\u643a\u5e26\u66f4\u591a\u4e0a\u4e0b\u6587\uff0c\u5e76\u4fbf\u4e8e\u533a\u5206\u9519\u8bef\u7c7b\u578b\u3002<\/p>\n<pre><code class=\"language-javascript\">\nclass AppError extends Error {\n  constructor(message, statusCode) {\n    super(message);\n    this.name = this.constructor.name;\n    this.statusCode = statusCode;\n    this.isOperational = true; \/\/ \u6807\u8bb0\u4e3a\u53ef\u64cd\u4f5c\u9519\u8bef\n    Error.captureStackTrace(this, this.constructor);\n  }\n}\n\nclass ValidationError extends AppError {\n  constructor(message) {\n    super(message, 400);\n  }\n}\n\nclass NotFoundError extends AppError {\n  constructor(message) {\n    super(message, 404);\n  }\n}\n<\/code><\/pre>\n<p><strong>\u6700\u4f73\u5b9e\u8df5<\/strong>\uff1a\u4e3a\u53ef\u9884\u89c1\u7684\u9519\u8bef\uff08\u5982\u53c2\u6570\u6821\u9a8c\u5931\u8d25\u3001\u8d44\u6e90\u4e0d\u5b58\u5728\uff09\u521b\u5efa\u81ea\u5b9a\u4e49\u9519\u8bef\u7c7b\uff0c\u5e76\u6807\u8bb0 <code>isOperational<\/code> \u4e3a true\u3002\u5bf9\u4e8e\u7a0b\u5e8f\u9519\u8bef\uff08\u5982 TypeError\uff09\uff0c\u5219\u4e0d\u5e94\u6807\u8bb0\uff0c\u4ee5\u4fbf\u5728\u5168\u5c40\u5904\u7406\u4e2d\u533a\u5206\u3002<\/p>\n<h2>3. \u96c6\u4e2d\u5f0f\u9519\u8bef\u5904\u7406<\/h2>\n<p>\u5728 Express \u7b49\u6846\u67b6\u4e2d\uff0c\u96c6\u4e2d\u5904\u7406\u9519\u8bef\u53ef\u4ee5\u907f\u514d\u91cd\u590d\u4ee3\u7801\uff0c\u5e76\u786e\u4fdd\u4e00\u81f4\u6027\u3002<\/p>\n<h3>3.1 Express \u9519\u8bef\u5904\u7406\u4e2d\u95f4\u4ef6<\/h3>\n<pre><code class=\"language-javascript\">\n\/\/ \u9519\u8bef\u5904\u7406\u4e2d\u95f4\u4ef6\uff084\u4e2a\u53c2\u6570\uff09\napp.use((err, req, res, next) =&gt; {\n  \/\/ \u8bb0\u5f55\u9519\u8bef\n  console.error(err);\n\n  \/\/ \u533a\u5206\u53ef\u64cd\u4f5c\u9519\u8bef\u4e0e\u7a0b\u5e8f\u9519\u8bef\n  if (err.isOperational) {\n    res.status(err.statusCode || 500).json({\n      status: &#x27;error&#x27;,\n      message: err.message\n    });\n  } else {\n    \/\/ \u7a0b\u5e8f\u9519\u8bef\uff1a\u4e0d\u6cc4\u9732\u7ec6\u8282\uff0c\u53d1\u9001\u901a\u7528\u6d88\u606f\n    res.status(500).json({\n      status: &#x27;error&#x27;,\n      message: &#x27;\u670d\u52a1\u5668\u5185\u90e8\u9519\u8bef&#x27;\n    });\n  }\n});\n<\/code><\/pre>\n<h3>3.2 \u4f7f\u7528 async\/await \u7684\u5305\u88c5\u51fd\u6570<\/h3>\n<p>Express \u4e0d\u4f1a\u81ea\u52a8\u6355\u83b7 async \u51fd\u6570\u4e2d\u7684\u9519\u8bef\uff0c\u9700\u8981\u624b\u52a8\u5305\u88c5\uff1a<\/p>\n<pre><code class=\"language-javascript\">\nconst asyncHandler = fn =&gt; (req, res, next) =&gt; {\n  Promise.resolve(fn(req, res, next)).catch(next);\n};\n\n\/\/ \u4f7f\u7528\napp.get(&#x27;\/user\/:id&#x27;, asyncHandler(async (req, res) =&gt; {\n  const user = await User.findById(req.params.id);\n  if (!user) {\n    throw new NotFoundError(&#x27;\u7528\u6237\u4e0d\u5b58\u5728&#x27;);\n  }\n  res.json(user);\n}));\n<\/code><\/pre>\n<p><strong>\ud83d\udca1 \u63d0\u793a<\/strong>\uff1a\u8bb8\u591a\u7b2c\u4e09\u65b9\u5e93\uff08\u5982 express-async-errors\uff09\u53ef\u4ee5\u81ea\u52a8\u5904\u7406\uff0c\u4f46\u4e86\u89e3\u539f\u7406\u5f88\u91cd\u8981\u3002<\/p>\n<h2>4. \u65e5\u5fd7\u8bb0\u5f55\u4e0e\u76d1\u63a7<\/h2>\n<p>\u9519\u8bef\u5904\u7406\u4e0d\u4ec5\u4ec5\u662f\u54cd\u5e94\u8bf7\u6c42\uff0c\u8fd8\u9700\u8981\u8bb0\u5f55\u548c\u76d1\u63a7\u3002\u4f7f\u7528 Winston \u6216 Pino \u7b49\u65e5\u5fd7\u5e93\uff0c\u5e76\u533a\u5206\u9519\u8bef\u7ea7\u522b\u3002<\/p>\n<pre><code class=\"language-javascript\">\nconst winston = require(&#x27;winston&#x27;);\n\nconst logger = winston.createLogger({\n  level: &#x27;info&#x27;,\n  format: winston.format.json(),\n  transports: [\n    new winston.transports.File({ filename: &#x27;error.log&#x27;, level: &#x27;error&#x27; }),\n    new winston.transports.Console({ format: winston.format.simple() })\n  ]\n});\n\n\/\/ \u5728\u9519\u8bef\u5904\u7406\u4e2d\u95f4\u4ef6\u4e2d\u8bb0\u5f55\napp.use((err, req, res, next) =&gt; {\n  logger.error({\n    message: err.message,\n    stack: err.stack,\n    url: req.originalUrl,\n    method: req.method,\n    ip: req.ip\n  });\n  \/\/ ...\n});\n<\/code><\/pre>\n<p><strong>\u6700\u4f73\u5b9e\u8df5<\/strong>\uff1a\u8bb0\u5f55\u9519\u8bef\u65f6\u5305\u542b\u8bf7\u6c42\u4e0a\u4e0b\u6587\uff08URL\u3001\u65b9\u6cd5\u3001IP\u3001\u7528\u6237ID\uff09\uff0c\u4fbf\u4e8e\u6392\u67e5\u3002\u751f\u4ea7\u73af\u5883\u5e94\u5c06\u65e5\u5fd7\u53d1\u9001\u5230\u96c6\u4e2d\u5f0f\u65e5\u5fd7\u7cfb\u7edf\uff08\u5982 ELK\u3001Sentry\uff09\u3002<\/p>\n<h2>5. \u4f18\u96c5\u964d\u7ea7\u4e0e\u8fdb\u7a0b\u7ba1\u7406<\/h2>\n<p>\u5bf9\u4e8e\u672a\u6355\u83b7\u7684\u5f02\u5e38\u548c\u672a\u5904\u7406\u7684 Promise \u62d2\u7edd\uff0c\u5e94\u8bb0\u5f55\u5e76\u4f18\u96c5\u5173\u95ed\u8fdb\u7a0b\u3002<\/p>\n<pre><code class=\"language-javascript\">\nprocess.on(&#x27;uncaughtException&#x27;, (err) =&gt; {\n  logger.error(&#x27;\u672a\u6355\u83b7\u5f02\u5e38:&#x27;, err);\n  \/\/ \u4f18\u96c5\u5173\u95ed\uff1a\u5173\u95ed\u670d\u52a1\u5668\u3001\u6570\u636e\u5e93\u8fde\u63a5\u7b49\n  server.close(() =&gt; {\n    process.exit(1);\n  });\n  \/\/ \u5982\u679c\u5173\u95ed\u5931\u8d25\uff0c\u5f3a\u5236\u9000\u51fa\n  setTimeout(() =&gt; process.exit(1), 10000);\n});\n\nprocess.on(&#x27;unhandledRejection&#x27;, (reason, promise) =&gt; {\n  logger.error(&#x27;\u672a\u5904\u7406\u7684 Promise \u62d2\u7edd:&#x27;, reason);\n  \/\/ \u6839\u636e\u60c5\u51b5\u51b3\u5b9a\u662f\u5426\u9000\u51fa\n});\n<\/code><\/pre>\n<p><strong>\u6ce8\u610f<\/strong>\uff1a<code>uncaughtException<\/code> \u540e\u8fdb\u7a0b\u72b6\u6001\u53ef\u80fd\u4e0d\u4e00\u81f4\uff0c\u5efa\u8bae\u8bb0\u5f55\u540e\u9000\u51fa\uff0c\u7531\u8fdb\u7a0b\u7ba1\u7406\u5668\uff08\u5982 PM2\uff09\u91cd\u542f\u3002<\/p>\n<h2>6. \u5e38\u89c1\u5751\u4e0e\u7ecf\u9a8c<\/h2>\n<ul>\n<li><strong>\u57511<\/strong>\uff1a\u5728\u56de\u8c03\u4e2d\u629b\u51fa\u5f02\u5e38\u4e0d\u4f1a\u88ab\u6355\u83b7\u3002\u5e94\u4f7f\u7528 <code>process.nextTick<\/code> \u6216\u76f4\u63a5\u4f20\u9012\u9519\u8bef\u3002<\/li>\n<li><strong>\u57512<\/strong>\uff1a\u5fd8\u8bb0 <code>return<\/code> \u5bfc\u81f4\u540e\u7eed\u4ee3\u7801\u6267\u884c\u3002\u5728\u9519\u8bef\u5904\u7406\u4e2d\u53ca\u65f6 <code>return<\/code>\u3002<\/li>\n<li><strong>\u57513<\/strong>\uff1a\u5728\u751f\u4ea7\u73af\u5883\u6cc4\u9732\u9519\u8bef\u5806\u6808\u3002\u59cb\u7ec8\u6839\u636e\u73af\u5883\u51b3\u5b9a\u662f\u5426\u8fd4\u56de\u8be6\u7ec6\u9519\u8bef\u4fe1\u606f\u3002<\/li>\n<li><strong>\u57514<\/strong>\uff1a\u5ffd\u7565 <code>unhandledRejection<\/code> \u4e8b\u4ef6\u3002\u81f3\u5c11\u8bb0\u5f55\u65e5\u5fd7\u3002<\/li>\n<\/ul>\n<h2>7. \u603b\u7ed3<\/h2>\n<p>\u672c\u6587\u4ecb\u7ecd\u4e86 Node.js \u9519\u8bef\u5904\u7406\u7684\u6838\u5fc3\u5b9e\u8df5\uff1a<\/p>\n<ul>\n<li>\u6839\u636e\u5f02\u6b65\u6a21\u5f0f\u9009\u62e9\u5408\u9002\u7684\u9519\u8bef\u5904\u7406\u65b9\u5f0f\uff08\u56de\u8c03\u3001Promise\u3001async\/await\uff09\u3002<\/li>\n<li>\u521b\u5efa\u81ea\u5b9a\u4e49\u9519\u8bef\u7c7b\uff0c\u533a\u5206\u53ef\u64cd\u4f5c\u9519\u8bef\u4e0e\u7a0b\u5e8f\u9519\u8bef\u3002<\/li>\n<li>\u4f7f\u7528\u96c6\u4e2d\u5f0f\u9519\u8bef\u5904\u7406\u4e2d\u95f4\u4ef6\uff0c\u7edf\u4e00\u54cd\u5e94\u683c\u5f0f\u3002<\/li>\n<li>\u8bb0\u5f55\u8be6\u7ec6\u65e5\u5fd7\u5e76\u76d1\u63a7\u9519\u8bef\u3002<\/li>\n<li>\u5904\u7406\u672a\u6355\u83b7\u5f02\u5e38\uff0c\u4f18\u96c5\u964d\u7ea7\u3002<\/li>\n<\/ul>\n<p>\u4e0b\u4e00\u6b65\uff0c\u4f60\u53ef\u4ee5\u63a2\u7d22\u4f7f\u7528 Sentry \u8fdb\u884c\u9519\u8bef\u8ffd\u8e2a\uff0c\u6216\u4f7f\u7528 OpenTelemetry \u5b9e\u73b0\u5206\u5e03\u5f0f\u8ffd\u8e2a\u3002\u5e0c\u671b\u8fd9\u4e9b\u5b9e\u8df5\u80fd\u5e2e\u52a9\u4f60\u6784\u5efa\u66f4\u5065\u58ee\u7684 Node.js \u5e94\u7528\u3002<\/p>\n<h2>\u5ef6\u4f38\u9605\u8bfb<\/h2>\n<ul>\n<li><a href=\"https:\/\/nodejs.org\/api\/errors.html\">Node.js \u5b98\u65b9\u6587\u6863\uff1a\u9519\u8bef\u5904\u7406<\/a><\/li>\n<li><a href=\"https:\/\/expressjs.com\/en\/guide\/error-handling.html\">Express \u9519\u8bef\u5904\u7406<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/winstonjs\/winston\">Winston \u65e5\u5fd7\u5e93<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u6df1\u5165\u63a2\u8ba8 Node.js \u9519\u8bef\u5904\u7406\u7684\u6700\u4f73\u5b9e\u8df5\uff0c\u6db5\u76d6\u56de\u8c03\u3001Promise\u3001async\/await \u7684\u9519\u8bef\u5904\u7406\u6a21\u5f0f\uff0c\u81ea\u5b9a\u4e49\u9519\u8bef\u7c7b\u3001\u96c6\u4e2d\u5f0f\u9519\u8bef\u7ba1\u7406\u3001\u65e5\u5fd7\u8bb0\u5f55\u4e0e\u4f18\u96c5\u964d\u7ea7\uff0c\u5e76\u63d0\u4f9b\u53ef\u8fd0\u884c\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u5e2e\u52a9\u4f60\u6784\u5efa\u5065\u58ee\u7684 Node.js \u5e94\u7528\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[44,5,103,45,144],"class_list":["post-291","post","type-post","status-publish","format-standard","hentry","category-3","category-4","tag-express","tag-node-js","tag-103","tag-45","tag-144"],"_links":{"self":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/291","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=291"}],"version-history":[{"count":0,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/291\/revisions"}],"wp:attachment":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}