{"id":1745,"date":"2017-08-09T14:10:40","date_gmt":"2017-08-09T06:10:40","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=1745"},"modified":"2017-08-09T14:10:40","modified_gmt":"2017-08-09T06:10:40","slug":"node-js-koa-%e6%a1%86%e6%9e%b6-%e7%9a%84%e5%9f%ba%e6%9c%ac%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=1745","title":{"rendered":"node.js Koa \u6846\u67b6 \u7684\u57fa\u672c\u7528\u6cd5"},"content":{"rendered":"<p>\n\tNode \u4e3b\u8981\u7528\u5728\u5f00\u53d1 Web \u5e94\u7528\u3002\u8fd9\u51b3\u5b9a\u4e86\u4f7f\u7528 Node\uff0c\u5f80\u5f80\u79bb\u4e0d\u5f00 Web \u5e94\u7528\u6846\u67b6\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\t<a href=\"http:\/\/koajs.com\/\" name=\"koajs\" target=\"_blank\" title=\"koajs\">Koa<\/a> \u5c31\u662f\u4e00\u79cd\u7b80\u5355\u597d\u7528\u7684 Web \u6846\u67b6\u3002\u5b83\u7684\u7279\u70b9\u662f\u4f18\u96c5\u3001\u7b80\u6d01\u3001\u8868\u8fbe\u529b\u5f3a\u3001\u81ea\u7531\u5ea6\u9ad8\u3002\u672c\u8eab\u4ee3\u7801\u53ea\u67091000\u591a\u884c\uff0c\u6240\u6709\u529f\u80fd\u90fd\u901a\u8fc7\u63d2\u4ef6\u5b9e\u73b0\uff0c\u5f88\u7b26\u5408 <a href=\"https:\/\/baike.baidu.com\/item\/unix\" name=\"unix\" target=\"_blank\" title=\"unix\">Unix<\/a> \u54f2\u5b66\u3002\n<\/p>\n<h2>\n\t\u5b89\u88c5<br \/>\n<\/h2>\n<p>\n\tKoa \u76ee\u524d\u9700\u8981 &gt;=0.11.x\u7248\u672c\u7684 node \u73af\u5883\u3002\u5e76\u9700\u8981\u5728\u6267\u884c node \u7684\u65f6\u5019\u9644\u5e26 &#8211;harmony \u6765\u5f15\u5165 generators \u3002 \u5982\u679c\u60a8\u5b89\u88c5\u4e86\u8f83\u65e7\u7248\u672c\u7684 node \uff0c\u60a8\u53ef\u4ee5\u5b89\u88c5 <a href=\"https:\/\/github.com\/tj\/n\" name=\"node\u7248\u672c\u63a7\u5236\u5668\" target=\"_blank\" title=\"node\u7248\u672c\u63a7\u5236\u5668\">n<\/a> (node\u7248\u672c\u63a7\u5236\u5668)\uff0c\u6765\u5feb\u901f\u5b89\u88c5 0.11.x\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\n$ npm install -g n\r\n$ n 0.11.12\r\n$ node --harmony my-koa-app.js<\/pre>\n<p>\n\t\u5982\u679c\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Node\uff0c\u90a3\u4e48\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684\u547d\u4ee4\u68c0\u67e5\u7248\u672c\u3002Koa \u5fc5\u987b\u4f7f\u7528 7.6 \u4ee5\u4e0a\u7684\u7248\u672c\u3002\u5982\u679c\u4f60\u7684\u7248\u672c\u4f4e\u4e8e\u8fd9\u4e2a\u8981\u6c42\uff0c\u5c31\u8981\u5148\u5347\u7ea7 Node\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\n$ node -v\r\nv8.0.0<\/pre>\n<h2>\n\t\u67b6\u8bbe HTTP \u670d\u52a1<br \/>\n<\/h2>\n<p>\n\t\u4e0a\u9762\u6211\u4eec\u5df2\u7ecf\u505a\u597d\u4e86\u524d\u671f\u7684\u51c6\u5907\u516c\u53f8\uff0c\u4e0b\u9762\u6211\u5c31\u5f00\u59cb\u7528 Koa \u67b6\u8bbe\u4e00\u4e2a HTTP \u670d\u52a1\u3002\n<\/p>\n<pre class=\"brush:jscript;\">\r\nconst Koa = require(&#039;koa&#039;);\r\nconst app = new Koa();\r\napp.listen(3000);<\/pre>\n<p>\n\t\u65b0\u5efa\u4e00\u4e2axttblog.js\u6587\u4ef6\uff0c\u5c06\u4e0a\u9762\u7684\u4ee3\u7801\u5199\u5165\u6587\u4ef6\uff0c\u8fd0\u884c\u4e0b\u9762\u7684\u547d\u4ee4\uff1a\n<\/p>\n<pre class=\"brush:jscript;\">\r\n$ node xttblog.js<\/pre>\n<p>\n\t\u6253\u5f00\u6d4f\u89c8\u5668\uff0c\u8bbf\u95ee http:\/\/127.0.0.1:3000 \u3002\u4f60\u4f1a\u770b\u5230\u9875\u9762\u663e\u793a&quot;Not Found&quot;\uff0c\u8868\u793a\u6ca1\u6709\u53d1\u73b0\u4efb\u4f55\u5185\u5bb9\u3002\u8fd9\u662f\u56e0\u4e3a\u6211\u4eec\u5e76\u6ca1\u6709\u544a\u8bc9 Koa \u5e94\u8be5\u663e\u793a\u4ec0\u4e48\u5185\u5bb9\u3002\n<\/p>\n<h2>\n\tContext \u5bf9\u8c61<br \/>\n<\/h2>\n<p>\n\tKoa \u63d0\u4f9b\u4e00\u4e2a Context \u5bf9\u8c61\uff0c\u8868\u793a\u4e00\u6b21\u5bf9\u8bdd\u7684\u4e0a\u4e0b\u6587\uff08\u5305\u62ec HTTP \u8bf7\u6c42\u548c HTTP \u56de\u590d\uff09\u3002\u901a\u8fc7\u52a0\u5de5\u8fd9\u4e2a\u5bf9\u8c61\uff0c\u5c31\u53ef\u4ee5\u63a7\u5236\u8fd4\u56de\u7ed9\u7528\u6237\u7684\u5185\u5bb9\u3002<br \/>\n\tContext.response.body\u5c5e\u6027\u5c31\u662f\u53d1\u9001\u7ed9\u7528\u6237\u7684\u5185\u5bb9\u3002\n<\/p>\n<p>\n\t\u6211\u4eec\u518d\u65b0\u5efa\u4e00\u4e2acodedq.js\uff0c\u5199\u5165\u4e0b\u9762\u7684\u4ee3\u7801\uff1a\n<\/p>\n<pre class=\"brush:jscript;\">\r\nconst Koa = require(&#039;koa&#039;);\r\nconst app = new Koa();\r\nconst main = ctx =&gt; {\r\n  ctx.response.body = &#039;Hello World&#039;;\r\n};\r\napp.use(main);\r\napp.listen(3000);<\/pre>\n<p>\n\t\u4e0a\u9762\u4ee3\u7801\u4e2d\uff0cmain\u51fd\u6570\u7528\u6765\u8bbe\u7f6ectx.response.body\u3002\u7136\u540e\uff0c\u4f7f\u7528app.use\u65b9\u6cd5\u52a0\u8f7dmain\u51fd\u6570\u3002<br \/>\n\t\u4f60\u53ef\u80fd\u5df2\u7ecf\u731c\u5230\u4e86\uff0cctx.response\u4ee3\u8868 HTTP Response\u3002\u540c\u6837\u5730\uff0cctx.request\u4ee3\u8868 HTTP Request\u3002\n<\/p>\n<p>\n\t\u7136\u540e\u5728\u6267\u884c\u4e0b\u9762\u7684\u547d\u4ee4\uff1a\n<\/p>\n<pre class=\"brush:bash;\">\r\n$ node codedq.js<\/pre>\n<p>\n\t\u8bbf\u95ee http:\/\/127.0.0.1:3000 \uff0c\u73b0\u5728\u5c31\u53ef\u4ee5\u770b\u5230&quot;Hello World&quot;\u4e86\u3002\n<\/p>\n<h2>\n\tHTTP Response \u7684\u7c7b\u578b<br \/>\n<\/h2>\n<p>\n\tKoa \u9ed8\u8ba4\u7684\u8fd4\u56de\u7c7b\u578b\u662ftext\/plain\uff0c\u5982\u679c\u60f3\u8fd4\u56de\u5176\u4ed6\u7c7b\u578b\u7684\u5185\u5bb9\uff0c\u53ef\u4ee5\u5148\u7528ctx.request.accepts\u5224\u65ad\u4e00\u4e0b\uff0c\u5ba2\u6237\u7aef\u5e0c\u671b\u63a5\u53d7\u4ec0\u4e48\u6570\u636e\uff08\u6839\u636e HTTP Request \u7684Accept\u5b57\u6bb5\uff09\uff0c\u7136\u540e\u4f7f\u7528ctx.response.type\u6307\u5b9a\u8fd4\u56de\u7c7b\u578b\u3002\n<\/p>\n<p>\n\t\u5728 ifxvn.js \u6587\u4ef6\u4e2d\u5199\u5165\u4e0b\u9762\u7684\u4ee3\u7801\uff1a\n<\/p>\n<pre class=\"brush:jscript;\">\r\nconst main = ctx =&gt; {\r\n  if (ctx.request.accepts(&#039;xml&#039;)) {\r\n    ctx.response.type = &#039;xml&#039;;\r\n    ctx.response.body = &#039;&lt;data&gt;Hello World&lt;\/data&gt;&#039;;\r\n  } else if (ctx.request.accepts(&#039;json&#039;)) {\r\n    ctx.response.type = &#039;json&#039;;\r\n    ctx.response.body = { data: &#039;Hello World&#039; };\r\n  } else if (ctx.request.accepts(&#039;html&#039;)) {\r\n    ctx.response.type = &#039;html&#039;;\r\n    ctx.response.body = &#039;&lt;p&gt;Hello World&lt;\/p&gt;&#039;;\r\n  } else {\r\n    ctx.response.type = &#039;text&#039;;\r\n    ctx.response.body = &#039;Hello World&#039;;\r\n  }\r\n}<\/pre>\n<p>\n\t\u7136\u540e\u8fd0\u884c\u8fd9\u4e2a\u6587\u4ef6\uff1a\n<\/p>\n<pre class=\"brush:bash;\">\r\n$ node ifxvn.js<\/pre>\n<p>\n\t\u8bbf\u95ee http:\/\/127.0.0.1:3000 \uff0c\u73b0\u5728\u770b\u5230\u7684\u5c31\u662f\u4e00\u4e2a XML \u6587\u6863\u4e86\u3002\n<\/p>\n<h2>\n\t\u7f51\u9875\u6a21\u677f<br \/>\n<\/h2>\n<p>\n\t\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u8fd4\u56de\u7ed9\u7528\u6237\u7684\u7f51\u9875\u5f80\u5f80\u90fd\u5199\u6210\u6a21\u677f\u6587\u4ef6\u3002\u6211\u4eec\u53ef\u4ee5\u8ba9 Koa \u5148\u8bfb\u53d6\u6a21\u677f\u6587\u4ef6\uff0c\u7136\u540e\u5c06\u8fd9\u4e2a\u6a21\u677f\u8fd4\u56de\u7ed9\u7528\u6237\u3002\u770b\u4e0b\u9762\u7684\u4ee3\u7801\uff1a\n<\/p>\n<pre class=\"brush:jscript;\">\r\nconst fs = require(&#039;fs&#039;);\r\nconst main = ctx =&gt; {\r\n  ctx.response.type = &#039;html&#039;;\r\n  ctx.response.body = fs.createReadStream(&#039;.\/hellword.html&#039;);\r\n};<\/pre>\n<p>\n\t\u8fd0\u884c\u8fd9\u4e2a ndislwf.js\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\n$ node ndislwf.js<\/pre>\n<p>\n\t\u8bbf\u95ee http:\/\/127.0.0.1:3000 \uff0c\u770b\u5230\u7684\u5c31\u662fhellword.html\u6587\u4ef6\u7684\u5185\u5bb9\u4e86\u3002\n<\/p>\n<p>\n\t\u9650\u4e8e\u7bc7\u5e45\uff0c\u6211\u4eec\u4e0b\u7ae0\u7ee7\u7eed\u5b66\u4e60 koa \u6846\u67b6\u7684\u8def\u7531\u529f\u80fd\u3002<\/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=1745\">node.js Koa \u6846\u67b6 \u7684\u57fa\u672c\u7528\u6cd5<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>Node \u4e3b\u8981\u7528\u5728\u5f00\u53d1 Web \u5e94\u7528\u3002\u8fd9\u51b3\u5b9a\u4e86\u4f7f\u7528 Node\uff0c\u5f80\u5f80\u79bb\u4e0d\u5f00 Web \u5e94\u7528\u6846\u67b6\u3002 Koa \u5c31\u662f\u4e00\u79cd [&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":[1106,1416,1415,48],"class_list":["post-1745","post","type-post","status-publish","format-standard","hentry","category-html5","tag-javascript","tag-koa","tag-node-js","tag-web"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1745","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=1745"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1745\/revisions"}],"predecessor-version":[{"id":1746,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1745\/revisions\/1746"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1745"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1745"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1745"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}