{"id":1743,"date":"2017-08-09T10:43:41","date_gmt":"2017-08-09T02:43:41","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=1743"},"modified":"2017-08-09T10:43:41","modified_gmt":"2017-08-09T02:43:41","slug":"%e9%97%b2%e8%b0%88node-js-%e7%9a%84koa%e6%a1%86%e6%9e%b6%e5%92%8cexpress%e6%a1%86%e6%9e%b6","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=1743","title":{"rendered":"\u95f2\u8c08Node.js \u7684Koa\u6846\u67b6\u548cExpress\u6846\u67b6"},"content":{"rendered":"<p>\n\t<a href=\"http:\/\/koa.bootcss.com\/\" name=\"koa\" target=\"_blank\" title=\"koa\">Koa<\/a> \u662f\u4e00\u4e2a\u57fa\u4e8e Node.js \u5e73\u53f0\u7684\u4e0b\u4e00\u4ee3 web \u5f00\u53d1\u6846\u67b6\u3002\u7f51\u4e0a\u5bf9 koa \u6846\u67b6\u7684\u5b66\u4e60\u6559\u7a0b\u5e76\u4e0d\u591a\uff0c\u56e0\u6b64\u6211\u6574\u7406\u4e86\u4e00\u4e9b\u5173\u4e8e koa \u6846\u67b6\u7684\u5b66\u4e60\u6559\u7a0b\uff0c\u65b9\u4fbf\u5927\u5bb6\u5b66\u4e60\uff01\n<\/p>\n<h2>\n\tkoa\u7b80\u4ecb<br \/>\n<\/h2>\n<p>\n\tkoa \u662f\u7531 <a href=\"http:\/\/expressjs.com\/\" name=\"expressjs\" target=\"_blank\" title=\"expressjs\">Express<\/a> \u539f\u73ed\u4eba\u9a6c\u6253\u9020\u7684\uff0c\u81f4\u529b\u4e8e\u6210\u4e3a\u4e00\u4e2a\u66f4\u5c0f\u3001\u66f4\u5bcc\u6709\u8868\u73b0\u529b\u3001\u66f4\u5065\u58ee\u7684 Web \u6846\u67b6\u3002\u4f7f\u7528 koa \u7f16\u5199 web \u5e94\u7528\uff0c\u901a\u8fc7\u7ec4\u5408\u4e0d\u540c\u7684 generator\uff0c\u53ef\u4ee5\u514d\u9664\u91cd\u590d\u7e41\u7410\u7684\u56de\u8c03\u51fd\u6570\u5d4c\u5957\uff0c\u5e76\u6781\u5927\u5730\u63d0\u5347\u9519\u8bef\u5904\u7406\u7684\u6548\u7387\u3002koa \u4e0d\u5728\u5185\u6838\u65b9\u6cd5\u4e2d\u7ed1\u5b9a\u4efb\u4f55\u4e2d\u95f4\u4ef6\uff0c\u5b83\u4ec5\u4ec5\u63d0\u4f9b\u4e86\u4e00\u4e2a\u8f7b\u91cf\u4f18\u96c5\u7684\u51fd\u6570\u5e93\uff0c\u4f7f\u5f97\u7f16\u5199 Web \u5e94\u7528\u53d8\u5f97\u5f97\u5fc3\u5e94\u624b\u3002\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"koa\u6846\u67b6\" src=\"http:\/\/ocuzluml6.bkt.clouddn.com\/koa.png\" style=\"width: 387px;height: 202px\" \/>\n<\/p>\n<p>\n\tkoa2 \u5b8c\u5168\u629b\u5f03\u4e86generator\u548cco\u7684\u505a\u6cd5\uff0c\u800c\u662f\u4f7f\u7528async\/await\u6765\u505a\u5f02\u6b65\u5f00\u53d1\u3002\n<\/p>\n<h2>\n\tkoa\u548cexpress\u7684\u4e0d\u540c\u70b9<br \/>\n<\/h2>\n<ul>\n<li>\n\t\tkoa\u548cexpress\u5728\u8868\u73b0\u4e0a\u7684\u4e00\u70b9\u4e0d\u540c\u662f\u91c7\u7528ctx\u4e00\u4e2a\u53c2\u6570\u6765\u8c03\u7528\u4e2d\u95f4\u4ef6\uff0c\u800c\u4e0d\u662fexpress\u7684req, res\u3002\n\t<\/li>\n<li>\n\t\t\u7136\u540e\u662f\u5728\u4e2d\u95f4\u4ef6\u8fde\u63a5\u7684\u65b9\u5f0f\u4e0a\u6709\u6240\u4e0d\u540c\u3002express\u7684\u8bbe\u8ba1\u662f\u4e32\u8054\u7684\uff0c\u8bbe\u8ba1\u601d\u8def\u8d85\u7ea7\u7b80\u6d01\u3002koa\u7684\u67d0\u4e00\u4e2a\u4e2d\u95f4\u4ef6\u53ef\u4ee5\u81ea\u884c\u9009\u62e9\u4e4b\u540e\u4e2d\u95f4\u4ef6\u7684\u6267\u884c\u4f4d\u7f6e\u7684\u3002\n\t<\/li>\n<\/ul>\n<h2>\n\t\u5b9e\u4f8b\u6bd4\u8f83<br \/>\n<\/h2>\n<p>\n\t\u6bd4\u5982\u4e00\u4e2a\u670d\u52a1\u5668\u5904\u7406\u65f6\u95f4\/\u65e5\u5fd7\u7684\u4e2d\u95f4\u4ef6\u7684\u5f00\u53d1\uff1a\n<\/p>\n<p>\n\texpress\u7684\u505a\u6cd5\n<\/p>\n<p>\n\trequest\u8fdb\u6765\uff0c\u8bb0\u5f55\u65f6\u95f4\u5230request._startTime\u4e0a\u3002\u7ed1\u5b9a\u4e00\u4e2a\u51fd\u6570\u5230response\u7684&#039;end&#039;,&#039;finish&#039;\u4ee5\u53caresponse.socket\u7684&#039;error&#039;,&#039;close&#039;\u4e8b\u4ef6\u4e0a\u3002\u90a3\u4e2a\u51fd\u6570\u4f1a\u7528\u5f53\u524d\u65f6\u95f4\u548cstartTime\u505a\u5dee\uff0c\u7b97\u51fa\u8fd0\u884c\u65f6\u95f4\u3002\n<\/p>\n<p>\n\tkoa2\u7684\u5b9e\u73b0\u4ee3\u7801\n<\/p>\n<pre class=\"brush:jscript;\">\r\nconst xTime = async (ctx,next)=&gt;{  \r\n\tlet start = new Date  \r\n\tawait next()  \r\n\tctx.set(&#039;X-Response-Time&#039;, (new Date) - start) + &#039;ms&#039;\uff09\r\n}<\/pre>\n<p>\n\t\u8fd9\u662f\u5728\u529f\u80fd\u5f00\u53d1\u4e0a\uff0c\u5728\u9519\u8bef\u5904\u7406\u4e0a\u7684\u53cb\u597d\u5ea6\u5c31\u66f4\u9ad8\u4e86\u3002<br \/>\n\t\u6ca1\u6709\u7279\u610f\u62b9\u9ed1express\u7684\u4e2d\u95f4\u4ef6\u5f00\u53d1\uff0c\u90a3\u4e2aexpress\u7248\u7684\u8ba1\u65f6\u5668\u662fexpress\u81ea\u5e26\u7684\u670d\u52a1\u5668\u65e5\u5fd7\u4e2d\u95f4\u4ef6morgan\u7684\u5b9e\u73b0\u3002\n<\/p>\n<h2>\n\t\u793e\u533a\u65b9\u9762<br \/>\n<\/h2>\n<p>\n\texpress\u7684\u793e\u533a\u8fd8\u662f\u5927\u3002koa\u672c\u6765\u5c31\u5c0f\uff0c\u8fd8\u88ab\u4ecekoa1\u8f6ckoa2\u4e00\u6298\u817e\uff0c\u5c31\u66f4\u5c0f\u4e86\u3002\u800c\u73b0\u5728koa2\u8fd8\u5728alpha\u7248\uff0ckoa\u53ca\u5176\u63d2\u4ef6\u7684\u5f00\u53d1\u901f\u5ea6\u6bd4\u8f83\u6ca1\u6709\u4fdd\u8bc1\u3002\u4f60\u627e\u5230\u4e00\u4e2a\u4e2d\u95f4\u4ef6\uff0c\u5982\u679c\u662fexpress\u7684\uff0c\u5f97\u8f6c\u8fc7\u6765\u3002\u5982\u679c\u662fkoa\u7684\uff0c\u4e00\u822c\u4e5f\u5f97\u518d\u8f6c\u4e00\u6b21\u3002\u6bd4\u5982\u6211\u4f7f\u7528\u7684passportjs\uff0cgithub\u6709koa\u7248koa-passport\uff0c\u5c31\u5206koa1\u7248\u672c\u548ckoa2\u7248\u672c\u7684\uff0c\u9009\u62e9\u65f6\u5f97\u6ce8\u610f\u4e00\u4e0b\u3002\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"\u57fa\u4e8enode.js\u7684koa\u6846\u67b6\" src=\"http:\/\/www.ruanyifeng.com\/blogimg\/asset\/2017\/bg2017080801.png\" style=\"width: 469px;height: 248px\" \/>\n<\/p>\n<p>\n\tkoa2\u597d\u7528\uff0c\u8bbe\u8ba1\u4e0a\u7684\u786e\u6709\u4f18\u52bf\u3002\u4f18\u52bf\u4e0d\u5728\u80fd\u5b9e\u73b0\u66f4\u5f3a\u7684\u529f\u80fd\uff0c\u800c\u662f\u53ef\u4ee5\u66f4\u7b80\u5355\u5730\u5b8c\u6210\u529f\u80fd\u3002koa2\u793e\u533a\u8fdc\u4e0d\u5982express\uff0c\u539f\u56e0\u4e4b\u4e00\u662f~~\u56e0\u4e3akoa2\u7684\u6b63\u5f0f\u7248\u79bb\u53d1\u884c\u4f30\u8ba1\u8fd8\u5f973~6\u4e2a\u6708\u3002~~koa\u7684\u6b63\u5f0f\u7248\u662fkoa1\u3002koa1\u5728\u601d\u60f3\u4e0a\u4e0ekoa2\u662f\u4e00\u81f4\u7684\uff0c\u4f46\u662fkoa2\u7684\u5b9e\u73b0\u66f4\u6f02\u4eae\u3002\u6709koa1\u5230koa2\u7684\u4e2d\u95f4\u4ef6\u5c01\u88c5\u51fd\u6570`koa-convert`\u3002\u800c\u4e00\u822c\u901a\u7528\u7684\u4e2d\u95f4\u4ef6\u90fd\u6709koa\u7248\uff0c\u6216\u8005\u80fd\u591f\u8f7b\u677e\u7684\u8f6c\u4e3akoa\u7248\u3002\n<\/p>\n<p>\n\tExpress \u548c Koa \u6700\u660e\u663e\u7684\u5dee\u522b\u5c31\u662f Handler \u7684\u5904\u7406\u65b9\u6cd5\uff0c\u4e00\u4e2a\u662f\u666e\u901a\u7684\u56de\u8c03\u51fd\u6570\uff0c\u4e00\u4e2a\u662f\u5229\u7528\u751f\u6210\u5668\u51fd\u6570\uff08Generator Function\uff09\u6765\u4f5c\u4e3a\u54cd\u5e94\u5668\u3002\u5f80\u91cc\u5934\u513f\u8bf4\u5c31\u662f Express \u662f\u5728\u540c\u4e00\u7ebf\u7a0b\u4e0a\u5b8c\u6210\u5f53\u524d\u8fdb\u7a0b\u7684\u6240\u6709 HTTP \u8bf7\u6c42\uff0c\u800c Koa \u5229\u7528 co \u4f5c\u4e3a\u5e95\u5c42\u8fd0\u884c\u6846\u67b6\uff0c\u5229\u7528 Generator \u7684\u7279\u6027\uff0c\u5b9e\u73b0&ldquo;\u534f\u7a0b\u54cd\u5e94&rdquo;\uff08\u5e76\u4e0d\u80fd\u5c06 Generator \u7b49\u4ef7\u4e8e\u534f\u7a0b\uff0c\u5728 V8 \u7684\u90ae\u4ef6\u5217\u8868\u4e2d\u5bf9 Generator \u7684\u5b9a\u4e49\u57fa\u672c\u662f `coroutine-like`\uff09\uff0c\u7136\u800c co \u8fd9\u4e2a\u5e93\u5bf9 Generator \u7684\u4f7f\u7528\u65b9\u6cd5\u5e76\u975e\u5f53\u521d Generator \u7684\u8bbe\u8ba1\u521d\u8877\u3002\n<\/p>\n<p>\n\texpress \u9002\u5408\u5165\u95e8\u9009\u624b\uff0ckoa\u9ad8\u4e0d\u6210\u4f4e\u4e0d\u5c31\uff0c\u5b66\u4e60\u4e00\u4e0b\u5176mw\u7684\u8bbe\u8ba1\u5373\u53ef\u3002\n<\/p>\n<h2>\n\t\u53c2\u8003\u8d44\u6599<br \/>\n<\/h2>\n<ul>\n<li>\n\t\t<a href=\"https:\/\/github.com\/koajs\/koa\/wiki\" name=\"koajs\" target=\"_blank\" title=\"koajs\">https:\/\/github.com\/koajs\/koa\/wiki<\/a>\n\t<\/li>\n<li>\n\t\t<a href=\"https:\/\/www.npmjs.com\/package\/koa-middlewares\" name=\"koa-middlewares\" target=\"_blank\" title=\"koa-middlewares\">https:\/\/www.npmjs.com\/package\/koa-middlewares<\/a>\n\t<\/li>\n<\/ul>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=1743\">\u95f2\u8c08Node.js \u7684Koa\u6846\u67b6\u548cExpress\u6846\u67b6<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>Koa \u662f\u4e00\u4e2a\u57fa\u4e8e Node.js \u5e73\u53f0\u7684\u4e0b\u4e00\u4ee3 web \u5f00\u53d1\u6846\u67b6\u3002\u7f51\u4e0a\u5bf9 koa \u6846\u67b6\u7684\u5b66\u4e60\u6559\u7a0b\u5e76\u4e0d\u591a\uff0c\u56e0\u6b64 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[1417,1416,1415],"class_list":["post-1743","post","type-post","status-publish","format-standard","hentry","category-html5","tag-express","tag-koa","tag-node-js"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1743","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1743"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1743\/revisions"}],"predecessor-version":[{"id":1744,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1743\/revisions\/1744"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1743"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1743"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}