{"id":2114,"date":"2020-12-22T22:02:27","date_gmt":"2020-12-22T14:02:27","guid":{"rendered":"https:\/\/www.yumefx.com\/?p=2114"},"modified":"2021-03-01T17:53:34","modified_gmt":"2021-03-01T09:53:34","slug":"node-js%e5%85%a5%e9%97%a8%ef%bc%88%e4%b8%8b%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.yumefx.com\/?p=2114","title":{"rendered":"Node.js\u5165\u95e8\uff08\u4e0b\uff09"},"content":{"rendered":"\n<p>\u672c\u6587\u4e3b\u8981\u8bf4\u4e00\u4e0b\u56de\u8c03\u51fd\u6570\u3001Promise\u4ee5\u53ca\u4e2d\u95f4\u4ef6\u7684\u6982\u5ff5\u4e0e\u7528\u6cd5\u3002<\/p>\n<h1>&nbsp;<\/h1>\n<h1 style=\"text-align: center;\">\u5f02\u6b65\u64cd\u4f5c\u4e0e\u56de\u8c03\u51fd\u6570<\/h1>\n<p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u6709\u4e9b\u64cd\u4f5c\u4f1a\u8017\u8d39\u4e00\u4e9b\u65f6\u95f4\u6765\u83b7\u5f97\u7ed3\u679c\uff0c\u4f8b\u5982\u6587\u4ef6\u8bfb\u5199\u3001\u7f51\u7edc\u8bf7\u6c42\u3001\u5ef6\u65f6\u51fd\u6570\u7b49\u7b49\uff0c\u8fd9\u79cd\u65f6\u5019\u5c31\u4f1a\u91c7\u53d6\u5f02\u6b65\u64cd\u4f5c\u907f\u514d\u963b\u585e\u7b49\u5f85\uff0c\u800c\u5982\u679c\u9700\u8981\u83b7\u53d6\u5f02\u6b65\u64cd\u4f5c\u7684\u7ed3\u679c\uff0c\u5fc5\u987b\u901a\u8fc7\u56de\u8c03\u51fd\u6570\u3002<\/p>\n\n\n\n<pre><span>var fs = require('fs')<\/span>\n<span><\/span>\n<span>console.log(1)<\/span>\n<span><\/span>\n<span>new Promise(function () {<\/span>\n<span>  console.log(2)<\/span>\n<span>  fs.readFile('.\/test.js', 'utf8', function (err,data) {<\/span>\n<span>    if (err) {<\/span>\n<span>      console.log(3)<\/span>\n<span>    } else {<\/span>\n<span>      console.log(4)<\/span>\n<span>    }<\/span>\n<span>  })<\/span>\n<span>  console.log(5)<\/span>\n<span>})<\/span>\n<span><\/span>\n<span>console.log(6)<\/span>\n<\/pre>\n\n\n\n<p>\u5f53\u5f02\u6b65\u64cd\u4f5c\u6210\u529f\u65f6\uff0cerr\u4e3anull\uff0cdata\u4e3a\u64cd\u4f5c\u7ed3\u679c\uff1b\u5f53\u5f02\u6b65\u64cd\u4f5c\u5931\u8d25\u65f6\uff0cerr\u4e3a\u62a5\u9519\u4fe1\u606f\uff0cdata\u4e3aundefined\u3002<\/p>\n<h1>&nbsp;<\/h1>\n<h1 style=\"text-align: center;\">Promise\u907f\u514d\u56de\u8c03\u5730\u72f1<\/h1>\n<p>\u5f53\u6709\u591a\u4e2a\u5f02\u6b65\u4efb\u52a1\u540c\u65f6\u8fdb\u884c\u65f6\uff0c\u65e0\u6cd5\u4fdd\u8bc1\u6bcf\u4e2a\u4efb\u52a1\u7684\u987a\u5e8f\uff0c\u5f53\u5176\u4e2d\u4e00\u4e2a\u5f02\u6b65\u4efb\u52a1\u4f9d\u8d56\u4e8e\u53e6\u4e00\u4e2a\u5f02\u6b65\u4efb\u52a1\u7684\u7ed3\u679c\u65f6\uff0c\u5c31\u4e0d\u5f97\u4e0d\u5199\u6210\u5d4c\u5957\u5f0f\u7ed3\u6784\uff0c\u5c42\u5c42\u5d4c\u5957\u65e2\u4e0d\u7f8e\u89c2\u4e5f\u96be\u4ee5\u7ef4\u62a4\uff0c\u8fd9\u79cd\u60c5\u51b5\u5c31\u88ab\u79f0\u4e3a\u201c\u56de\u8c03\u5730\u72f1\u201d\u3002<\/p>\n<h3>&nbsp;<\/h3>\n<h3>Promise\u4f7f\u7528<\/h3>\n<p>\u800c\u4e3a\u4e86\u907f\u514d\u56de\u8c03\u5730\u72f1\uff0c\u5728EcmaScript6\u4e2d\uff0c\u65b0\u589e\u4e86Promise\u8fd9\u4e2aAPI\uff0c\u5b83\u662f\u4e00\u4e2a\u5bb9\u7eb3\u5f02\u6b65\u4efb\u52a1\u7684\u5bb9\u5668\u3002<\/p>\n\n\n\n<pre><span>var fs = require('fs')<\/span>\n<span><\/span>\n<span>console.log(1)<\/span>\n<span><\/span>\n<span>new Promise(function () {<\/span>\n<span>  console.log(2)<\/span>\n<span>  fs.readFile('.\/test.js', 'utf8', function (err,data) {<\/span>\n<span>    if (err) {<\/span>\n<span>      console.log(3)<\/span>\n<span>    } else {<\/span>\n<span>      console.log(4)<\/span>\n<span>    }<\/span>\n<span>  })<\/span>\n<span>  console.log(5)<\/span>\n<span>})<\/span>\n<span><\/span>\n<span>console.log(6)<\/span>\n<\/pre>\n\n\n\n<p>\u6700\u7ec8\u4f1a\u51fa\u73b0\u7684\u7ed3\u679c\u987a\u5e8f\u662f12564\u3002<\/p>\n<p>\u4ece\u4e0a\u9762\u7684\u987a\u5e8f\u53ef\u4ee5\u770b\u51fa\uff0cPromise\u867d\u7136\u662f\u5f02\u6b65\u4efb\u52a1\u7684\u5bb9\u5668\uff0c\u4f46\u5176\u672c\u8eab\u662f\u540c\u6b65\u4efb\u52a1\uff0c\u4e00\u65e6\u521b\u5efa\u7acb\u5373\u5f00\u59cb\u8fd0\u884c\uff0c\u56e0\u6b64Promise\u4e00\u822c\u4f5c\u4e3a\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff0c\u5728\u9700\u8981\u8c03\u7528\u65f6\u6267\u884c\u3002<\/p>\n<p>Promise\u5bb9\u5668\u9ed8\u8ba4\u662fPending\u72b6\u6001\uff0c\u800c\u8fd0\u884c\u540e\u53ef\u80fd\u51fa\u73b0\u4ee5\u4e0b\u4e24\u79cd\u72b6\u6001\u3002<\/p>\n<ul>\n<li>\u901a\u8fc7resolve(data)\u6539\u53d8\u6210fulfilled\u72b6\u6001\uff0c\u8fd4\u56de\u7ed3\u679c<\/li>\n<li>\u901a\u8fc7reject(err)\u6539\u53d8\u6210Rejected\u72b6\u6001\uff0c\u8fd4\u56de\u5f02\u5e38\u3002<\/li>\n<\/ul>\n\n\n\n<pre><span>var fs = require('fs')<\/span>\n<span><\/span>\n<span>var P1 = () =&gt; new Promise(function (resolve, reject) {<\/span>\n<span>  fs.readFile('.\/a.js', 'utf8', function (err,data) {<\/span>\n<span>    if (err) {<\/span>\n<span>      reject(err)<\/span>\n<span>    } else {<\/span>\n<span>      resolve(data)<\/span>\n<span>    }<\/span>\n<span>  })<\/span>\n<span>})<\/span>\n<span><\/span>\n<span>\/\/ \u5bf9\u7ed3\u679c\u8fdb\u884c\u5904\u7406\uff0c\u7b2c\u4e8c\u4e2a\u5f02\u5e38\u56de\u8c03\u53ef\u4ee5\u7701\u7565<\/span>\n<span>P1().then(function (data) {<\/span>\n<span>  console.log(data)<\/span>\n<span>}, function (err) {<\/span>\n<span>  console.log('failed')<\/span>\n<span>})<\/span>\n<span><\/span>\n<span>\/\/ \u6216\u8005\u4f7f\u7528then\u6765\u5904\u7406\u6b63\u5e38\u7ed3\u679c\uff0c\u4f7f\u7528catch\u6765\u5904\u7406\u5f02\u5e38<\/span>\n<span>P1().then(function (data) {<\/span>\n<span>  console.log(err.message)<\/span>\n<span>})<\/span>\n<span>.catch(function (err) {<\/span>\n<span>  console.log(err.message)<\/span>\n<span>})<\/span>\n<\/pre>\n\n\n\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662fPromise\u7684\u72b6\u6001\u6539\u53d8\u4e4b\u540e\u624d\u4f1a\u6267\u884cthen\u65b9\u6cd5\u3002<\/p>\n<h3>&nbsp;<\/h3>\n<h3>then\u7684\u5b9e\u8d28<\/h3>\n<p>\u4e0a\u9762\u6f14\u793a\u4e86\u4f7f\u7528then\u6765\u5904\u7406\u5f02\u6b65\u56de\u8c03\u7684\u7528\u6cd5\uff0c\u4e0b\u9762\u5219\u8981\u8bf4\u660ethen\u6700\u70e7\u8111\u7684\u5730\u65b9\u2014\u2014then\u7684\u8fd4\u56de\u503c\u4e5f\u662fPromise\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u672a\u8fd0\u884c\u7684Pending\u72b6\u6001\uff0c\u53ef\u7ee7\u7eed\u94fe\u5f0f\u8fde\u63a5then\u6267\u884c\u3002<\/p>\n<p>then\u8fd4\u56de\u7684Promise\u5bf9\u8c61\u7684\u72b6\u6001\u53ef\u4ee5\u5728\u6d4f\u89c8\u5668\u4e2d\u65ad\u70b9\u8c03\u8bd5\u72b6\u6001\u8fdb\u884c\u67e5\u770b\uff0c\u6b64\u5904\u4e0d\u518d\u8d58\u8ff0\uff0c\u53ef\u67e5\u770b\u6b64<a href=\"https:\/\/www.bilibili.com\/video\/BV1454y1R7vj?p=7\">\u6559\u7a0b<\/a>\u3002<\/p>\n\n\n\n<pre><span>const p = () =&gt; new Promise((resolve,reject) =&gt; {<\/span>\n<span>  resolve('123')<\/span>\n<span>})<\/span>\n<span><\/span>\n<span>p().then((value)=&gt;{<\/span>\n<span>    console.log(value)   \/\/123<\/span>\n<span>  },(reason)=&gt;{<\/span>\n<span>    console.log(reason)<\/span>\n<span>})<\/span>\n<span>.then((value)=&gt;{<\/span>\n<span>    console.log('test')  \/\/test<\/span>\n<span>    console.log(value)   \/\/undefined<\/span>\n<span>  },(reason)=&gt;{<\/span>\n<span>    console.log(reason)<\/span>\n<span>})<\/span>\n<\/pre>\n\n\n\n<p>\u4ece\u4e0a\u9762\u7684\u7ed3\u679c\u53ef\u4ee5\u770b\u51fa\uff0c\u7b2c\u4e00\u4e2athen\u5e76\u672aresolve\uff0c\u4f46\u4ecd\u7136\u89e6\u53d1\u4e86\u7b2c\u4e8c\u4e2athen\uff0c\u8bf4\u660e\u7b2c\u4e00\u4e2athen\u8fd4\u56de\u7684Promise\u5bf9\u8c61\u5728\u4e0d\u8c03\u7528resolve()\u7684\u60c5\u51b5\u4e0b\uff0c\u4f1a\u5728\u8fd0\u884c\u5b8c\u540e\u9ed8\u8ba4\u81ea\u52a8resolve\u4e14\u4e0d\u8fd4\u56de\u7ed3\u679c\uff08\u56e0\u4e3athen\u4e2d\u6ca1\u6709resolve()\u548creject()\u65b9\u6cd5\uff09\u3002<\/p>\n<p>\u800c\u5728then\u4e2d\u4e5f\u53ef\u4ee5\u901a\u8fc7return\u8fd4\u56de\u7ed3\u679c\uff0c\u800c\u6b64\u65f6then\u7684\u8fd4\u56de\u503c\u5c06\u662f\u4e00\u4e2afulfilled\u7684Promise\u5bf9\u8c61\uff0c\u5e76\u89e6\u53d1\u4e0b\u4e00\u4e2athen\u7684\u6b63\u5e38\u5904\u7406\u65b9\u6cd5\u3002<\/p>\n\n\n\n<pre><span>const p = () =&gt; new Promise((resolve,reject) =&gt; {<\/span>\n<span>  resolve('123')<\/span>\n<span>})<\/span>\n<span><\/span>\n<span>p().then((value)=&gt;{<\/span>\n<span>    console.log(value) \/\/123<\/span>\n<span>    return(value)<\/span>\n<span>  },(reason)=&gt;{<\/span>\n<span>    console.log(reason)<\/span>\n<span>})<\/span>\n<span>.then((value)=&gt;{<\/span>\n<span>    console.log('test') \/\/test<\/span>\n<span>    console.log(value)  \/\/123<\/span>\n<span>  },(reason)=&gt;{<\/span>\n<span>  console.log(reason)<\/span>\n<span>})<\/span>\n<\/pre>\n\n\n\n<p>\u800c\u5728then\u4e2d\u8fd0\u884c\u7684\u4ee3\u7801\u5982\u679c\u62a5\u9519\uff0c\u5219\u5f53\u524dthen\u7684\u8fd4\u56de\u503c\u5c06\u662f\u4e00\u4e2arejected\u7684Promise\u5bf9\u8c61\uff0c\u5e76\u4e14\u89e6\u53d1\u4e0b\u4e00\u4e2athen\u7684\u5f02\u5e38\u5904\u7406\u65b9\u6cd5\u3002<\/p>\n\n\n\n<pre><span>const p = () =&gt; new Promise((resolve,reject) =&gt; {<\/span>\n<span>  reject('123')<\/span>\n<span>})<\/span>\n<span><\/span>\n<span>p().then((value)=&gt;{<\/span>\n<span>    console.log(value)<\/span>\n<span>  },(reason)=&gt;{<\/span>\n<span>    console.log(rs)<\/span>\n<span>})<\/span>\n<span>.then((value)=&gt;{<\/span>\n<span>    console.log(value)<\/span>\n<span>  },(reason)=&gt;{<\/span>\n<span>    console.log('test')          \/\/test<\/span>\n<span>    console.log(reason.message)  \/\/rs is not defined<\/span>\n<span>})<\/span>\n<\/pre>\n\n\n\n<p><strong>\u603b\u7ed3\u4e00\u4e0b\uff0c\u4e00\u4e2anew Promise\u5fc5\u987b\u8fd0\u884c\u5e76\u4fee\u6539\u72b6\u6001\u540e\u624d\u80fd\u89e6\u53d1\u7b2c\u4e00\u4e2athen\uff0c\u800cthen\u5f97\u5230\u7684\u8fd4\u56de\u503cpromise\u5bf9\u8c61\u5219\u8fd0\u884c\u540e\u81ea\u52a8\u4fee\u6539\u72b6\u6001\u5e76\u89e6\u53d1\u4e0b\u4e00\u4e2athen\uff0c\u76f4\u5230\u6574\u6761then\u94fe\u5f0f\u7ed3\u675f\u3002<\/strong><\/p>\n<p>\u73b0\u5728\u6765\u770bcatch\u65b9\u6cd5\uff0c\u5176\u5b9e\u5c31\u662fthat\u7684\u8bed\u6cd5\u7cd6\uff0ccatch(do(err))\u7b49\u4ef7\u4e8ethat(undefined, do(err))\uff0c\u800cfinally(do(data))\u7b49\u4ef7\u4e8ethat(do(data), do(err))\u3002<\/p>\n<p>\u90a3\u4e48\u4e0b\u9762\u6765\u770b\u4e00\u4e2a\u4f8b\u5b50\uff0c\u5728\u6d4f\u89c8\u5668\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u4ee3\u7801\u4f1a\u5f97\u5230\u4e0d\u4e00\u6837\u7684\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre><span>fetch('\/').then((res)=&gt;res.text()).then(data=&gt;console.log(data.length))<\/span>\n<span>\/\/ 29450<\/span>\n<span>fetch('\/').then((res)=&gt;res.text().length)<\/span>\n<span>\/\/ undefined<\/span>\n<\/pre>\n\n\n\n<p>fetch\u547d\u4ee4\u7684\u8fd4\u56de\u503c\u662f\u4e00\u4e2aPromise\u5bf9\u8c61\uff0c\u800cres.text()\u8fd4\u56de\u503c\u4e5f\u662f\u4e00\u4e2aPromise\u5bf9\u8c61\uff0cPromise\u5bf9\u8c61\u6ca1\u6709length\u5c5e\u6027\uff0c\u56e0\u6b64\u7b2c\u4e8c\u79cd\u65b9\u5f0f\u81ea\u7136\u662f\u9519\u8bef\u7684\u3002<\/p>\n<h3>&nbsp;<\/h3>\n<h3>Promise\u5904\u7406\u56de\u8c03\u5730\u72f1<\/h3>\n<p>\u800c\u5982\u679c\u8981\u5b9e\u73b0\u987a\u5e8f\u8c03\u7528\uff0c\u5219\u53ef\u4ee5\u6839\u636e\u94fe\u5f0f\u6267\u884c\uff0c\u5728P1\u6210\u529f\u7684\u56de\u8c03\u51fd\u6570\u4e2d\u8fd4\u56deP2\uff0c\u518d\u7531P2\u5411\u4e0b\u8fd0\u884c\uff0c\u5982\u4e0b\u3002<\/p>\n\n\n\n<pre><span>var fs = require('fs')<\/span>\n<span><\/span>\n<span>var P1 = () =&gt; new Promise(function (resolve, reject) {<\/span>\n<span>  fs.readFile('.\/a.js', 'utf8', function (err,data) {<\/span>\n<span>    if (err) {<\/span>\n<span>      reject(err)<\/span>\n<span>    } else {<\/span>\n<span>      resolve(data)<\/span>\n<span>    }<\/span>\n<span>  })<\/span>\n<span>})<\/span>\n<span><\/span>\n<span>var P2 = () =&gt; new Promise(function (resolve, reject) {<\/span>\n<span>  fs.readFile('.\/app.js', 'utf8', function (err,data) {<\/span>\n<span>    if (err) {<\/span>\n<span>      reject(err)<\/span>\n<span>    } else {<\/span>\n<span>      resolve(data)<\/span>\n<span>    }<\/span>\n<span>  })<\/span>\n<span>})<\/span>\n<span><\/span>\n<span>\/\/ P1.then()\u6267\u884c\u6210\u529f\u65f6\u8fd4\u56deP2\u7684\u5bf9\u8c61\uff0c\u518d\u94fe\u5f0fP2.then()<\/span>\n<span>P1().then(function (data) {<\/span>\n<span>  console.log(data)<\/span>\n<span>  return P2()<\/span>\n<span>}, function (err) {<\/span>\n<span>  console.log('failed')<\/span>\n<span>})<\/span>\n<span>.then(function (data) {<\/span>\n<span>  console.log(data)<\/span>\n<span>})<\/span>\n<\/pre>\n\n\n\n<h3>&nbsp;<\/h3>\n<h3>async&amp;await<\/h3>\n<p>async&amp;await\u5176\u5b9e\u662fPromise&amp;then\u7684\u8bed\u6cd5\u7cd6\uff0c\u80fd\u591f\u66f4\u52a0\u76f4\u89c2\u5730\u5904\u7406\u56de\u8c03\u5730\u72f1\u3002<\/p>\n<p>\u57fa\u672c\u4f7f\u7528\u65b9\u5f0f\u5982\u4e0b\u3002<\/p>\n\n\n\n<pre><span>var fs = require('fs')<\/span>\n<span><\/span>\n<span>var P1 = () =&gt; new Promise(function (resolve, reject) {<\/span>\n<span>  fs.readFile('.\/a.js', 'utf8', function (err,data) {<\/span>\n<span>    if (err) {<\/span>\n<span>      reject(err)<\/span>\n<span>    } else {<\/span>\n<span>      resolve(data)<\/span>\n<span>    }<\/span>\n<span>  })<\/span>\n<span>})<\/span>\n<span><\/span>\n<span>async function Q1() {<\/span>\n<span>  var res = await P1()<\/span>\n<span>  console.log(res)<\/span>\n<span>}<\/span>\n<span><\/span>\n<span>Q1()<\/span>\n<\/pre>\n\n\n\n<p>\u5728\u5904\u7406\u56de\u8c03\u5730\u72f1\u65f6\u7684\u8fd0\u884c\u8fc7\u7a0b\u5982\u4e0b<\/p>\n\n\n\n<pre><span>var P1 = (count) =&gt; new Promise(function (resolve, reject) {<\/span>\n<span>  console.log(1)<\/span>\n<span>  setTimeout(function(){<\/span>\n<span>    console.log(count)<\/span>\n<span>    resolve(count+1)<\/span>\n<span>  },1000)<\/span>\n<span>  console.log(2)<\/span>\n<span>})<\/span>\n<span><\/span>\n<span>async function Q1() {<\/span>\n<span>  console.log(3)<\/span>\n<span>  var res1 = await P1(100)<\/span>\n<span>  console.log(4)<\/span>\n<span>  var res2 = await P1(res1)<\/span>\n<span>  console.log(5)<\/span>\n<span>}<\/span>\n<span><\/span>\n<span>Q1()<\/span>\n<span>console.log(6)<\/span>\n<\/pre>\n\n\n\n<p>\u6700\u7ec8\u7ed3\u679c\u662f\u7acb\u5373\u8f93\u51fa3\u30011\u30012\u30016\uff0c\u7ecf\u8fc71\u79d2\u540e\u8f93\u51fa100\u30014\u30011\u30012\uff0c\u518d\u8fc71\u79d2\u540e\u8f93\u51fa101\u30015\u3002<\/p>\n<p>\u4ece\u987a\u5e8f\u53ef\u4ee5\u770b\u51faasync\u4fee\u9970\u7684\u65b9\u6cd5\u5c31\u662f\u987a\u5e8f\u8c03\u7528\u94fe\u672c\u8eab\uff0c\u800c\u4e14\u4e0d\u963b\u585e\u65b9\u6cd5\u540e\u65b9\u4ee3\u7801\u7684\u8fd0\u884c\uff0c\u65b9\u6cd5\u5185\u90e8\u5219\u4f1a\u7b49\u5f85\u5f02\u6b65\u4efb\u52a1\u7684\u7ed3\u679c\uff0c\u5e76\u4e14\u6309\u7167\u987a\u5e8f\u6267\u884c\u5f02\u6b65\u4efb\u52a1\u3002<\/p>\n<p>\u4f46\u662f\u8fd9\u79cdasync&amp;await\u65b9\u5f0f\u6709\u4e00\u4e2a\u7f3a\u70b9\u5c31\u662fawait\u53ea\u80fd\u83b7\u53d6resolve\u4f20\u51fa\u7684\u503c\u7ed3\u679c\uff0c\u5982\u679c\u8c03\u7528\u94fe\u4e2d\u51fa\u73b0\u9519\u8bef\u5c31\u53ea\u80fd\u901a\u8fc7try&amp;catch\u83b7\u5f97\u3002<\/p>\n\n\n\n<pre><span>async function Q1() {<\/span>\n<span>  try{<\/span>\n<span>    var res1 = await P1(100)<\/span>\n<span>    var res2 = await P1(res1)<\/span>\n<span>  }catch(e){<\/span>\n<span>    console.log(e)<\/span>\n<span>  }<\/span>\n<span>}<\/span>\n<\/pre>\n\n\n\n<h1>&nbsp;<\/h1>\n<h1 style=\"text-align: center;\">\u4e2d\u95f4\u4ef6<\/h1>\n<p>\u4e2d\u95f4\u4ef6\u7684\u672c\u8d28\u5c31\u662f\u4e00\u4e2a\u8bf7\u6c42\u5904\u7406\u65b9\u6cd5\uff0c\u4ee5express\u4e3a\u4f8b\uff0c\u628a\u7528\u6237\u4ece\u8bf7\u6c42\u5230\u54cd\u5e94\u7684\u6574\u4e2a\u8fc7\u7a0b\u5206\u53d1\u5230\u591a\u4e2a\u4e2d\u95f4\u4ef6\u4e2d\u53bb\u5904\u7406\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u7684\u7075\u6d3b\u6027\u548c\u52a8\u6001\u6269\u5c55\u6027\u3002<\/p>\n<h3>&nbsp;<\/h3>\n<h3>\u5339\u914d\u4e2d\u95f4\u4ef6<\/h3>\n\n\n\n<pre><span>app.use(function(req,res,next){<\/span>\n<span>  console.log(1)<\/span>\n<span>  next()<\/span>\n<span>})<\/span>\n<span><\/span>\n<span>app.use(function(req,res,next){<\/span>\n<span>  console.log(2)<\/span>\n<span>})<\/span>\n<span><\/span>\n<span>app.use(function(req,res,next){<\/span>\n<span>  console.log(3)<\/span>\n<span>})<\/span>\n<\/pre>\n\n\n\n<p>\u4e0a\u9762\u8fd9\u79cd\u60c5\u51b5\u662f\u5168\u5c40\u5339\u914d\uff0c\u6240\u6709\u8bbf\u95ee\u90fd\u4f1a\u8fdb\u5165app.use\u8fdb\u884c\u5904\u7406\uff0c\u800c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u5339\u914d\u6210\u529f\u4e00\u6b21\u540e\u5c31\u4e0d\u4f1a\u518d\u5c1d\u8bd5\u5339\u914d\u4e0b\u9762\u7684\u4e24\u4e2aapp.use\uff0c\u9664\u975e\u6709next()\u4f20\u9012\u5230\u4e0b\u4e00\u4e2aapp.use\u3002\u56e0\u6b64\uff0c\u4e0a\u9762\u7684\u7ed3\u679c\u662f1\u30012\uff0c\u7b2c\u4e09\u4e2aapp.use\u751a\u81f3\u540e\u9762\u7684app.get\u7b49\u90fd\u4f1a\u5931\u6548\u3002<\/p>\n<p>\u56e0\u4e3a\u4f1a\u5339\u914d\u6240\u6709\u8bf7\u6c42\uff0c\u56e0\u6b64\u4e00\u822c\u7528\u6765\u5904\u7406404\u9519\u8bef\uff0c\u4f4d\u7f6e\u653e\u5728\u5176\u4ed6\u8def\u7531\u4e4b\u540e\u3002<\/p>\n<p>\u5f53\u5339\u914d\u591a\u6b21\u65f6\uff0c\u591a\u6b21\u5904\u7406\u7684\u662f\u540c\u4e00\u4e2a\u8bf7\u6c42\u548c\u54cd\u5e94\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3a\u94fe\u5f0f\u6267\u884c\u3002<\/p>\n\n\n\n<pre><span>app.use('\/a',function(req,res,next){<\/span>\n<span>  console.log(2)<\/span>\n<span>  next()<\/span>\n<span>})<\/span>\n<span>app.use('\/b',function(req,res,next){<\/span>\n<span>  console.log(3)<\/span>\n<span>})<\/span>\n<\/pre>\n\n\n\n<p>\u8fd9\u79cd\u60c5\u51b5\u4e0b\u662f\u9650\u5b9a\u5339\u914d\uff0c\u4f8b\u5982app.use(&#8216;\/a&#8217;)\u4ee3\u8868\u5339\u914d\u4ee5&#8221;\/a\/&#8221;\u5f00\u5934\u7684\u8bf7\u6c42\uff0c\u4f8b\u5982127.0.0.1\/a\/sdf\uff0c\u7ed3\u679c\u4e3a2\uff0c\u5339\u914d\u6210\u529f\u4e00\u6b21\u540e\u4e5f\u4e0d\u4f1a\u518d\u5339\u914d\u540e\u9762\u7684\u4e2d\u95f4\u4ef6\uff0c\u56e0\u6b64next()\u7684\u4f7f\u7528\u662f\u8981\u6ce8\u610f\u7684\u3002<\/p>\n<p>\u800capp.get\u4ee5\u53caapp.post\u7b49\u5176\u5b9e\u5c31\u662f\u7279\u6b8a\u7684app.use\uff0c\u79f0\u4e3a\u8def\u7531\u7ea7\u522b\u4e2d\u95f4\u4ef6\uff0c\u533a\u522b\u53ea\u662f\u5339\u914d\u5b8c\u6574\u8def\u5f84\u5e76\u4e14\u9650\u5b9a\u8bf7\u6c42\u65b9\u5f0f\u800c\u5df2\u3002<\/p>\n<h3>&nbsp;<\/h3>\n<h3>\u5168\u5c40\u9519\u8bef\u5904\u7406\u4e2d\u95f4\u4ef6<\/h3>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u4f1a\u5728\u5355\u4e2a\u5339\u914d\u4e2d\u95f4\u4ef6\u4e2d\u5904\u7406\u9519\u8bef\uff0c\u4f46\u4e5f\u53ef\u4ee5\u901a\u8fc7next\u5c06\u9519\u8bef\u53d1\u9001\u5230\u5168\u5c40\u9519\u8bef\u5904\u7406\u4e2d\u95f4\u4ef6\u8fdb\u884c\u7edf\u4e00\u5904\u7406\uff0c\u56e0\u6b64\u4f4d\u7f6e\u4e5f\u5e94\u8be5\u653e\u5728\u5176\u4ed6\u8def\u7531\u4e4b\u540e\u3002<\/p>\n\n\n\n<pre><span>app.get('\/', function (req, res, next) {<\/span>\n<span>  fs.readFile('test',function (err, data) {<\/span>\n<span>    if (err) {<\/span>\n<span>      \/\/ \u9ed8\u8ba4\u60c5\u51b5\u4e0b\u7684\u5904\u7406\u62a5\u9519<\/span>\n<span>      \/\/ return res.status(500).send('Server Error')<\/span>\n<span>      \/\/ \u4f20\u9001\u5230\u5168\u5c40\u9519\u8bef<\/span>\n<span>      next(err)<\/span>\n<span>    }<\/span>\n<span>  })<\/span>\n<span>})<\/span>\n<span>app.use(function (err, req, req, next) {<\/span>\n<span>  console.log(err.message)<\/span>\n<span>})<\/span>\n<\/pre>\n\n\n\n<p>\u5176\u4ed6\u8fd8\u6709\u5185\u7f6e\u4e2d\u95f4\u4ef6\u548c\u7b2c\u4e09\u65b9\u4e2d\u95f4\u4ef6\u7b49\uff0c\u53ef\u81ea\u884c\u4e86\u89e3\u3002<\/p>\n<p>\u00a0<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u4ed6\u4eec\u8bd5\u56fe\u628a\u4f60\u57cb\u4e86\uff0c<\/p>\n<p>\u4f46\u4f60\u8981\u8bb0\u5f97\u4f60\u662f\u79cd\u5b50\u3002<\/p>\n<p style=\"text-align: right;\">\u2014\u2014\u58a8\u897f\u54e5\u8c1a\u8bed<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u4e3b\u8981\u8bf4\u4e00\u4e0b\u56de\u8c03\u51fd\u6570\u3001Promise\u4ee5\u53ca\u4e2d\u95f4\u4ef6\u7684\u6982\u5ff5\u4e0e\u7528\u6cd5\u3002 &nbsp; \u5f02\u6b65\u64cd\u4f5c\u4e0e\u56de\u8c03\u51fd\u6570 \u5728\u5b9e\u9645\u5f00\u53d1\u4e2d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2110,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[18,4],"tags":[31,14,32,22],"_links":{"self":[{"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/posts\/2114"}],"collection":[{"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2114"}],"version-history":[{"count":0,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/posts\/2114\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/media\/2110"}],"wp:attachment":[{"href":"https:\/\/www.yumefx.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2114"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}