{"id":1753,"date":"2017-08-09T16:14:07","date_gmt":"2017-08-09T08:14:07","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=1753"},"modified":"2017-08-09T16:14:07","modified_gmt":"2017-08-09T08:14:07","slug":"node-js-koa-%e6%a1%86%e6%9e%b6-%e7%9a%84cookies%e3%80%81%e8%a1%a8%e5%8d%95%e3%80%81%e6%96%87%e4%bb%b6%e4%b8%8a%e4%bc%a0%e5%8a%9f%e8%83%bd%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=1753","title":{"rendered":"node.js Koa \u6846\u67b6 \u7684Cookies\u3001\u8868\u5355\u3001\u6587\u4ef6\u4e0a\u4f20\u529f\u80fd\u8be6\u89e3"},"content":{"rendered":"<p>\n\tkoa \u6846\u67b6\u5bf9cookie\u7684\u64cd\u4f5c\u4e5f\u6709\u5c01\u88c5\u3002\u800c\u4e14api\u4e5f\u76f8\u5f53\u7684\u7b80\u5355\u3002\n<\/p>\n<p>\n\t\u6211\u4eec\u53ef\u4ee5\u6839\u636eContext\u63d0\u4f9b\u7684\u4e24\u4e2a\u65b9\u6cd5\u6765\u5bf9cookie\u8fdb\u884c\u8bbf\u95ee\u548c\u4fee\u6539\u3002\n<\/p>\n<h2>\n\tctx.cookies.get(name, [options])<br \/>\n<\/h2>\n<p>\n\t\u83b7\u5f97 cookie \u4e2d\u540d\u4e3a name \u7684\u503c\uff0coptions \u4e3a\u53ef\u9009\u53c2\u6570\uff1a\n<\/p>\n<ul>\n<li>\n\t\t&#039;signed&#039;: \u5982\u679c\u4e3a true\uff0c\u8868\u793a\u8bf7\u6c42\u65f6 cookie \u9700\u8981\u8fdb\u884c\u7b7e\u540d\u3002\n\t<\/li>\n<\/ul>\n<h2>\n\tctx.cookies.set(name, value, [options])<br \/>\n<\/h2>\n<p>\n\t\u8bbe\u7f6e cookie \u4e2d\u540d\u4e3a name \u7684\u503c\uff0coptions \u4e3a\u53ef\u9009\u53c2\u6570\uff1a\n<\/p>\n<ul>\n<li>\n\t\tsigned: \u662f\u5426\u8981\u505a\u7b7e\u540d\n\t<\/li>\n<li>\n\t\texpires: cookie \u6709\u6548\u671f\u65f6\u95f4\n\t<\/li>\n<li>\n\t\tpath: cookie \u7684\u8def\u5f84\uff0c\u9ed8\u8ba4\u4e3a \/&#039;\n\t<\/li>\n<li>\n\t\tdomain: cookie \u7684\u57df\n\t<\/li>\n<li>\n\t\tsecure: false \u8868\u793a cookie \u901a\u8fc7 HTTP \u534f\u8bae\u53d1\u9001\uff0ctrue \u8868\u793a cookie \u901a\u8fc7 HTTPS \u53d1\u9001\u3002\n\t<\/li>\n<li>\n\t\thttpOnly: true \u8868\u793a cookie \u53ea\u80fd\u901a\u8fc7 HTTP \u534f\u8bae\u53d1\u9001\n\t<\/li>\n<\/ul>\n<p>\n\t\u6ce8\u610f\uff1aKoa \u4f7f\u7528\u4e86 Express \u7684 <a href=\"https:\/\/github.com\/pillarjs\/cookies\" name=\"pillarjs cookies\" target=\"_blank\" title=\"pillarjs cookies\">cookies<\/a> \u6a21\u5757\uff0coptions \u53c2\u6570\u53ea\u662f\u7b80\u5355\u5730\u76f4\u63a5\u8fdb\u884c\u4f20\u9012\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\tctx.cookies\u7528\u6765\u8bfb\u5199 Cookie\u3002\u770b\u4e0b\u9762\u7684\u8bb0\u5f55\u7f51\u9875\u6d4f\u89c8\u6b21\u6570\u7684\u4f8b\u5b50\uff1a\n<\/p>\n<pre class=\"brush:jscript;\">\r\nconst main = function(ctx) {\r\n  const n = Number(ctx.cookies.get(&#039;view&#039;) || 0) + 1;\r\n  ctx.cookies.set(&#039;view&#039;, n);\r\n  ctx.response.body = n + &#039; views&#039;;\r\n}<\/pre>\n<p>\n\t\u8fd0\u884c\u4e0a\u9762\u7684demo\uff0c\u6ca1\u5237\u65b0\u4e00\u6b21\u9875\u9762\uff0cviews\u6bcf\u6b21\u90fd\u4f1a\u8ba1\u6570\u589e\u52a01\u3002\n<\/p>\n<h2>\n\t\u8868\u5355\u5904\u7406<br \/>\n<\/h2>\n<p>\n\tWeb \u5e94\u7528\u79bb\u4e0d\u5f00\u5904\u7406\u8868\u5355\u3002\u672c\u8d28\u4e0a\uff0c\u8868\u5355\u5c31\u662f POST \u65b9\u6cd5\u53d1\u9001\u5230\u670d\u52a1\u5668\u7684\u952e\u503c\u5bf9\u3002<a href=\"https:\/\/www.npmjs.com\/package\/koa-body\" name=\"koa-body\" target=\"_blank\" title=\"koa-body\">koa-body<\/a>\u6a21\u5757\u53ef\u4ee5\u7528\u6765\u4ece POST \u8bf7\u6c42\u7684\u6570\u636e\u4f53\u91cc\u9762\u63d0\u53d6\u952e\u503c\u5bf9\u3002\n<\/p>\n<p>\n\t\u770b\u4e0b\u9762\u7684 body.js \u6587\u4ef6\u4e2d\u7684\u4ee3\u7801\uff1a\n<\/p>\n<pre class=\"brush:jscript;\">\r\nconst koaBody = require(&#039;koa-body&#039;);\r\nconst main = async function(ctx) {\r\n  const body = ctx.request.body;\r\n  if (!body.name) ctx.throw(400, &#039;.name required&#039;);\r\n  ctx.body = { name: body.name };\r\n};\r\napp.use(koaBody());<\/pre>\n<p>\n\t\u90e8\u7f72\u8fd9\u4e2a\u6587\u4ef6\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\n$ node body.js<\/pre>\n<p>\n\t\u6253\u5f00\u53e6\u4e00\u4e2a\u547d\u4ee4\u884c\u7a97\u53e3\uff0c\u8fd0\u884c\u4e0b\u9762\u7684\u547d\u4ee4\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\n$ curl -X POST --data &quot;name=xttblog&quot; 127.0.0.1:3000\r\n{&quot;name&quot;:&quot;xttblog&quot;}\r\n\r\n$ curl -X POST --data &quot;name&quot; 127.0.0.1:3000\r\nname required<\/pre>\n<p>\n\t\u4e0a\u9762\u4ee3\u7801\u4f7f\u7528 POST \u65b9\u6cd5\u5411\u670d\u52a1\u5668\u53d1\u9001\u4e00\u4e2a\u952e\u503c\u5bf9\uff0c\u4f1a\u88ab\u6b63\u786e\u89e3\u6790\u3002\u5982\u679c\u53d1\u9001\u7684\u6570\u636e\u4e0d\u6b63\u786e\uff0c\u5c31\u4f1a\u6536\u5230\u9519\u8bef\u63d0\u793a\u3002\n<\/p>\n<h2>\n\t\u6587\u4ef6\u4e0a\u4f20<br \/>\n<\/h2>\n<p>\n\t\u57fa\u672c\u4e0a\u6240\u6709\u7684\u6846\u67b6\u90fd\u4f1a\u5bf9\u6587\u4ef6\u4e0a\u4f20\u6709\u6240\u5c01\u88c5\u3002koa \u6846\u67b6\u4e5f\u4e0d\u4f8b\u5916\u3002\n<\/p>\n<p>\n\t<a href=\"https:\/\/www.npmjs.com\/package\/koa-body\" name=\"koa-body\" target=\"_blank\" title=\"koa-body\">koa-body<\/a>\u6a21\u5757\u8fd8\u53ef\u4ee5\u7528\u6765\u5904\u7406\u6587\u4ef6\u4e0a\u4f20\u3002\n<\/p>\n<p>\n\t\u770b\u4e0b\u9762\u7684 file.js \u6587\u4ef6\u4e2d\u7684\u4ee3\u7801\uff1a\n<\/p>\n<pre class=\"brush:jscript;\">\r\nconst os = require(&#039;os&#039;);\r\nconst path = require(&#039;path&#039;);\r\nconst koaBody = require(&#039;koa-body&#039;);\r\nconst main = async function(ctx) {\r\n  const tmpdir = os.tmpdir();\r\n  const filePaths = [];\r\n  const files = ctx.request.body.files || {};\r\n  for (let key in files) {\r\n    const file = files[key];\r\n    const filePath = path.join(tmpdir, file.name);\r\n    const reader = fs.createReadStream(file.path);\r\n    const writer = fs.createWriteStream(filePath);\r\n    reader.pipe(writer);\r\n    filePaths.push(filePath);\r\n  }\r\n  ctx.body = filePaths;\r\n};\r\napp.use(koaBody({ multipart: true }));<\/pre>\n<p>\n\t\u90e8\u7f72\u4e0a\u9762\u7684 file.js \u6587\u4ef6\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\n$ node file.js<\/pre>\n<p>\n\t\u6253\u5f00\u53e6\u4e00\u4e2a\u547d\u4ee4\u884c\u7a97\u53e3\uff0c\u8fd0\u884c\u4e0b\u9762\u7684\u547d\u4ee4\uff0c\u4e0a\u4f20\u4e00\u4e2a\u6587\u4ef6\u3002\u6ce8\u610f\uff0c\/path\/to\/file\u8981\u66f4\u6362\u4e3a\u771f\u5b9e\u7684\u6587\u4ef6\u8def\u5f84\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\n$ curl --form upload=@\/path\/to\/file http:\/\/127.0.0.1:3000\r\n[\"\/tmp\/file\"]<\/pre>\n<p>\n\t\u7136\u540e\u6211\u4eec\u5230\/tmp\/file\u6587\u4ef6\u76ee\u5f55\u4e2d\u67e5\u770b\u53d1\u73b0\u6587\u4ef6\u5df2\u7ecf\u6210\u529f\u4e0a\u4f20\u4e86\u3002\u5230\u8fd9\u91cc\uff0ckoa\u6846\u67b6\u7684\u6240\u6709\u5185\u5bb9\u6211\u4eec\u90fd\u5b66\u4e60\u5b8c\u6210\u4e86\u3002\u9644\u4e0a\u524d\u9762\u51e0\u7bc7\u6587\u7ae0\u7684\u5730\u5740\uff1a\n<\/p>\n<ul>\n<li>\n\t\t<a href=\"http:\/\/www.xttblog.com\/?p=1743\" name=\"\u95f2\u8c08Node.js \u7684Koa\u6846\u67b6\u548cExpress\u6846\u67b6\" target=\"_blank\" title=\"\u95f2\u8c08Node.js \u7684Koa\u6846\u67b6\u548cExpress\u6846\u67b6\">\u95f2\u8c08Node.js \u7684Koa\u6846\u67b6\u548cExpress\u6846\u67b6<\/a>\n\t<\/li>\n<li>\n\t\t<a href=\"http:\/\/www.xttblog.com\/?p=1745\" name=\"node.js Koa \u6846\u67b6 \u7684\u57fa\u672c\u7528\u6cd5\" target=\"_blank\" title=\"node.js Koa \u6846\u67b6 \u7684\u57fa\u672c\u7528\u6cd5\">node.js Koa \u6846\u67b6 \u7684\u57fa\u672c\u7528\u6cd5<\/a>\n\t<\/li>\n<li>\n\t\t<a href=\"http:\/\/www.xttblog.com\/?p=1747\" name=\"node.js Koa \u6846\u67b6 \u7684\u8def\u7531\u7528\u6cd5\" target=\"_blank\" title=\"node.js Koa \u6846\u67b6 \u7684\u8def\u7531\u7528\u6cd5\">node.js Koa \u6846\u67b6 \u7684\u8def\u7531\u7528\u6cd5<\/a>\n\t<\/li>\n<li>\n\t\t<a href=\"http:\/\/www.xttblog.com\/?p=1749\" name=\"node.js Koa \u6846\u67b6 \u7684\u4e2d\u95f4\u4ef6\u7528\u6cd5\" target=\"_blank\" title=\"node.js Koa \u6846\u67b6 \u7684\u4e2d\u95f4\u4ef6\u7528\u6cd5\">node.js Koa \u6846\u67b6 \u7684\u4e2d\u95f4\u4ef6\u7528\u6cd5<\/a>\n\t<\/li>\n<li>\n\t\t<a href=\"http:\/\/www.xttblog.com\/?p=1751\" name=\"node.js Koa \u6846\u67b6 \u7684\u9519\u8bef\u5904\u7406\" target=\"_blank\" title=\"node.js Koa \u6846\u67b6 \u7684\u9519\u8bef\u5904\u7406\">node.js Koa \u6846\u67b6 \u7684\u9519\u8bef\u5904\u7406<\/a>\n\t<\/li>\n<li>\n\t\t<a href=\"http:\/\/www.xttblog.com\/?p=1753\" name=\"node.js Koa \u6846\u67b6 \u7684Cookies\u3001\u8868\u5355\u3001\u6587\u4ef6\u4e0a\u4f20\u529f\u80fd\u8be6\u89e3\" target=\"_blank\" title=\"node.js Koa \u6846\u67b6 \u7684Cookies\u3001\u8868\u5355\u3001\u6587\u4ef6\u4e0a\u4f20\u529f\u80fd\u8be6\u89e3\">node.js Koa \u6846\u67b6 \u7684Cookies\u3001\u8868\u5355\u3001\u6587\u4ef6\u4e0a\u4f20\u529f\u80fd\u8be6\u89e3<\/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=1753\">node.js Koa \u6846\u67b6 \u7684Cookies\u3001\u8868\u5355\u3001\u6587\u4ef6\u4e0a\u4f20\u529f\u80fd\u8be6\u89e3<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>koa \u6846\u67b6\u5bf9cookie\u7684\u64cd\u4f5c\u4e5f\u6709\u5c01\u88c5\u3002\u800c\u4e14api\u4e5f\u76f8\u5f53\u7684\u7b80\u5355\u3002 \u6211\u4eec\u53ef\u4ee5\u6839\u636eContext\u63d0\u4f9b\u7684\u4e24\u4e2a\u65b9\u6cd5\u6765 [&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,251],"class_list":["post-1753","post","type-post","status-publish","format-standard","hentry","category-html5","tag-koa","tag-node-js","tag-web","tag-251"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1753","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=1753"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1753\/revisions"}],"predecessor-version":[{"id":1754,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1753\/revisions\/1754"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}