{"id":1747,"date":"2017-08-09T14:43:57","date_gmt":"2017-08-09T06:43:57","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=1747"},"modified":"2017-08-09T14:43:57","modified_gmt":"2017-08-09T06:43:57","slug":"node-js-koa-%e6%a1%86%e6%9e%b6-%e7%9a%84%e8%b7%af%e7%94%b1%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=1747","title":{"rendered":"node.js Koa \u6846\u67b6 \u7684\u8def\u7531\u7528\u6cd5"},"content":{"rendered":"<p>\n\t\u7f51\u7ad9\u4e00\u822c\u90fd\u6709\u591a\u4e2a\u9875\u9762\u3002\u901a\u8fc7ctx.request.path\u53ef\u4ee5\u83b7\u53d6\u7528\u6237\u8bf7\u6c42\u7684\u8def\u5f84\uff0c\u7531\u6b64\u5b9e\u73b0\u7b80\u5355\u7684\u8def\u7531\u3002\n<\/p>\n<p>\n\t\u6240\u8c13\u7684\u8def\u5f84\uff0c\u4e5f\u5c31\u662f\u591a\u4e2a\u9875\u9762\u8fdb\u884c\u76f8\u4e92\u95f4\u7684\u8df3\u8f6c\u3002\n<\/p>\n<p>\n\tKoa Request \u5bf9\u8c61\u662f\u5bf9 node \u7684 request \u8fdb\u4e00\u6b65\u62bd\u8c61\u548c\u5c01\u88c5\uff0c\u63d0\u4f9b\u4e86\u65e5\u5e38 HTTP \u670d\u52a1\u5668\u5f00\u53d1\u4e2d\u4e00\u4e9b\u6709\u7528\u7684\u529f\u80fd\u3002\u5173\u4e8eRequest\u5bf9\u8c61\u7684\u66f4\u591a\u7528\u6cd5\uff0c\u5927\u5bb6\u53ef\u4ee5\u53c2\u8003<a href=\"http:\/\/koajs.com\/\" name=\"koajs\" target=\"_blank\" title=\"koajs\">\u5b98\u65b9\u6587\u6863<\/a>\u3002\n<\/p>\n<p>\n\t\u770b\u4e0b\u9762\u7684luyou.js\u4e2d\u7684\u4ee3\u7801\uff1a\n<\/p>\n<pre class=\"brush:jscript;\">\r\nconst main = ctx =&gt; {\r\n  if (ctx.request.path !== &#039;\/&#039;) {\r\n    ctx.response.type = &#039;html&#039;;\r\n    ctx.response.body = &#039;&lt;a href=&quot;\/&quot;&gt;Index Page&lt;\/a&gt;&#039;;\r\n  } else {\r\n    ctx.response.body = &#039;Hello World&#039;;\r\n  }\r\n};<\/pre>\n<p>\n\t\u7136\u540e\u8fd0\u884c\u4e0a\u9762\u7684\u4ee3\u7801\uff1a\n<\/p>\n<pre class=\"brush:bash;\">\r\n$ node luyou.js<\/pre>\n<p>\n\t\u8bbf\u95ee http:\/\/127.0.0.1:3000\/about \uff0c\u53ef\u4ee5\u770b\u5230\u4e00\u4e2a\u94fe\u63a5\uff0c\u70b9\u51fb\u540e\u5c31\u8df3\u5230\u9996\u9875\u3002\n<\/p>\n<p>\n\t\u4e0a\u9762\u7684\u8def\u7531\uff0c\u6211\u4eec\u7edf\u79f0\u95ee\u539f\u751f\u8def\u7531\u3002\n<\/p>\n<h2>\n\tkoa-route \u6a21\u5757<br \/>\n<\/h2>\n<p>\n\t\u539f\u751f\u8def\u7531\u7528\u8d77\u6765\u4e0d\u592a\u65b9\u4fbf\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5c01\u88c5\u597d\u7684koa-route\u6a21\u5757\u3002\n<\/p>\n<p>\n\t\u770b\u4e0b\u9762\u7684route.js\u6587\u4ef6\u4e2d\u7684\u4ee3\u7801\uff1a\n<\/p>\n<pre class=\"brush:jscript;\">\r\nconst route = require(&#039;koa-route&#039;);\r\nconst about = ctx =&gt; {\r\n  ctx.response.type = &#039;html&#039;;\r\n  ctx.response.body = &#039;&lt;a href=&quot;\/&quot;&gt;Index Page&lt;\/a&gt;&#039;;\r\n};\r\nconst main = ctx =&gt; {\r\n  ctx.response.body = &#039;Hello World&#039;;\r\n};\r\napp.use(route.get(&#039;\/&#039;, main));\r\napp.use(route.get(&#039;\/about&#039;, about));<\/pre>\n<p>\n\t\u4e0a\u9762\u4ee3\u7801\u4e2d\uff0c\u6839\u8def\u5f84\/\u7684\u5904\u7406\u51fd\u6570\u662fmain\uff0c\/about\u8def\u5f84\u7684\u5904\u7406\u51fd\u6570\u662fabout\u3002\n<\/p>\n<p>\n\t\u7136\u540e\u540c\u6837\u7684\u65b9\u5f0f\uff0c\u8fd0\u884c\u8fd9\u4e2aroute.js\u3002\u6548\u679c\u548c\u539f\u751f\u8def\u7531\u4e00\u81f4\u3002\n<\/p>\n<h2>\n\t\u9759\u6001\u8d44\u6e90<br \/>\n<\/h2>\n<p>\n\t\u5982\u679c\u7f51\u7ad9\u63d0\u4f9b\u9759\u6001\u8d44\u6e90\uff08\u56fe\u7247\u3001\u5b57\u4f53\u3001\u6837\u5f0f\u8868\u3001\u811a\u672c&#8230;&#8230;\uff09\uff0c\u4e3a\u5b83\u4eec\u4e00\u4e2a\u4e2a\u5199\u8def\u7531\u5c31\u5f88\u9ebb\u70e6\uff0c\u4e5f\u6ca1\u5fc5\u8981\u3002<a href=\"https:\/\/www.npmjs.com\/package\/koa-static\" name=\"koa-static\" target=\"_blank\" title=\"koa-static\">koa-static<\/a>\u6a21\u5757\u5c01\u88c5\u4e86\u8fd9\u90e8\u5206\u7684\u8bf7\u6c42\u3002\n<\/p>\n<p>\n\t\u770b\u4e0b\u9762\u8fd9\u4e2a static.js \u6587\u4ef6\uff1a\n<\/p>\n<pre class=\"brush:jscript;\">\r\nconst path = require(&#039;path&#039;);\r\nconst serve = require(&#039;koa-static&#039;);\r\nconst main = serve(path.join(__dirname));\r\napp.use(main);<\/pre>\n<p>\n\t\u8fd0\u884c\u4e0a\u9762\u8fd9\u4e2ademo\uff0c\u8bbf\u95ee http:\/\/127.0.0.1:3000\/12.js\uff0c\u5728\u6d4f\u89c8\u5668\u91cc\u5c31\u53ef\u4ee5\u770b\u5230\u8fd9\u4e2a\u811a\u672c\u7684\u5185\u5bb9\u3002\n<\/p>\n<h2>\n\t\u91cd\u5b9a\u5411<br \/>\n<\/h2>\n<p>\n\t\u6709\u4e9b\u573a\u5408\uff0c\u670d\u52a1\u5668\u9700\u8981\u91cd\u5b9a\u5411\uff08redirect\uff09\u8bbf\u95ee\u8bf7\u6c42\u3002\u6bd4\u5982\uff0c\u7528\u6237\u767b\u9646\u4ee5\u540e\uff0c\u5c06\u4ed6\u91cd\u5b9a\u5411\u5230\u767b\u9646\u524d\u7684\u9875\u9762\u3002ctx.response.redirect()\u65b9\u6cd5\u53ef\u4ee5\u53d1\u51fa\u4e00\u4e2a302\u8df3\u8f6c\uff0c\u5c06\u7528\u6237\u5bfc\u5411\u53e6\u4e00\u4e2a\u8def\u7531\u3002\n<\/p>\n<p>\n\t\u4e0b\u9762\u662f&nbsp;redirect.js \u6587\u4ef6\u4e2d\u7684\u4ee3\u7801\uff1a\n<\/p>\n<pre class=\"brush:jscript;\">\r\n\/\/ redirect.js\r\nconst redirect = ctx =&gt; {\r\n  ctx.response.redirect(&#039;\/&#039;);\r\n  ctx.response.body = &#039;&lt;a href=&quot;\/&quot;&gt;Index Page&lt;\/a&gt;&#039;;\r\n};\r\napp.use(route.get(&#039;\/redirect&#039;, redirect));<\/pre>\n<p>\n\t\u540c\u6837\u7684\u8fd0\u884c\u8fd9\u4e2ademo\uff0c\u8bbf\u95ee http:\/\/127.0.0.1:3000\/redirect \uff0c\u6d4f\u89c8\u5668\u4f1a\u5c06\u7528\u6237\u5bfc\u5411\u6839\u8def\u7531\u3002\n<\/p>\n<p>\n\t\u9650\u4e8e\u7bc7\u5e45\uff0c\u6211\u5c06\u4e2d\u95f4\u4ef6\uff08middleware\uff09\u7684\u5185\u5bb9\u653e\u5230\u4e86\u4e0b\u4e00\u7ae0\u3002\u6211\u4eec\u4e0b\u4e00\u7ae0\u518d\u89c1\uff01<\/p>\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=1747\">node.js Koa \u6846\u67b6 \u7684\u8def\u7531\u7528\u6cd5<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u7f51\u7ad9\u4e00\u822c\u90fd\u6709\u591a\u4e2a\u9875\u9762\u3002\u901a\u8fc7ctx.request.path\u53ef\u4ee5\u83b7\u53d6\u7528\u6237\u8bf7\u6c42\u7684\u8def\u5f84\uff0c\u7531\u6b64\u5b9e\u73b0\u7b80\u5355\u7684\u8def\u7531\u3002 \u6240\u8c13\u7684 [&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":[1416,1415,48],"class_list":["post-1747","post","type-post","status-publish","format-standard","hentry","category-html5","tag-koa","tag-node-js","tag-web"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1747","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=1747"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1747\/revisions"}],"predecessor-version":[{"id":1748,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1747\/revisions\/1748"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1747"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1747"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}