{"id":279,"date":"2021-08-01T18:36:49","date_gmt":"2021-08-01T10:36:49","guid":{"rendered":"http:\/\/gjweb.top\/?p=279"},"modified":"2021-08-01T18:36:50","modified_gmt":"2021-08-01T10:36:50","slug":"koa2%e6%8e%a5%e5%8f%a3%e7%bc%96%e5%86%99","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=279","title":{"rendered":"Koa2\u63a5\u53e3\u7f16\u5199"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">GET\u683c\u5f0f\u53c2\u6570\u63a5\u6536<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>const router = require('koa-router')()<br>router.prefix('\/api')<br>router.get(\"\/list\",,async (ctx)=&gt;{<br> &nbsp; &nbsp;let query = ctx.puery<br> &nbsp; &nbsp;console.log(\"\u53c2\u6570\u63a5\u6536\",query)<br> &nbsp; &nbsp;ctx.body = {<br> &nbsp; &nbsp; &nbsp; &nbsp;code:101,<br> &nbsp; &nbsp; &nbsp; &nbsp;data:&#91;<br> &nbsp; &nbsp;        {name:'zhangsan',age:18},<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  {name:'wangwu',,age:19}<br> &nbsp;      ]<br> &nbsp;  }<br>})<br>module.exports = router<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">POST\u683c\u5f0f\u63a5\u6536(\u652f\u6301JSON \\ form-data \\ x-www-form-urlencoded )<\/h2>\n\n\n\n<p>\u5728 app.js \u6587\u4ef6\u4e2d\u914d\u7f6e\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const bodyparser = require('koa-body')<br>app.use(bodyparser({<br> &nbsp;enableTypes:&#91;'json', 'form', 'text'],<br> &nbsp;multipart:true<br>}))<\/code><\/pre>\n\n\n\n<p>\u5b89\u88c5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install koa-body --save<\/code><\/pre>\n\n\n\n<p>\u5728\u8def\u7531\u4e2d\u4f7f\u7528<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const router = require('koa-router')()<br>router.prefix('\/api')<br>router.post(\"\/create\", async (ctx)=&gt;{<br> &nbsp; &nbsp;let body = ctx.request.body;<br> &nbsp; &nbsp;console.log(\"\u6253\u5370post\u53c2\u6570\",body)<br> &nbsp; &nbsp;ctx.body=\"hello world\"<br>})<br>module.exports = router<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u63a5\u6536\u6587\u4ef6\u4e0a\u4f20<\/h2>\n\n\n\n<p>\u914d\u7f6e\u540cPOST\u65b9\u5f0f\u4e00\u6837,\u8bed\u6cd5\u540c\u4e0a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const fs = require(\"fs\")<br>const path = require(\"path\")<br>const router = require('koa-router')()<br>router.prefix('\/api')<br>\u200b<br>router.post(\"\/create\", async (ctx)=&gt;{<br> &nbsp; &nbsp;let body = ctx.request.body;<br> &nbsp; &nbsp;console.log(\"\u989d\u5916\u7684\u6587\u4ef6\u53c2\u6570\")<br> &nbsp; &nbsp;const file = ctx.request.files.file; \/\/ \u83b7\u53d6\u4e0a\u4f20\u6587\u4ef6<br> &nbsp; &nbsp;\/\/ \u521b\u5efa\u53ef\u8bfb\u6d41<br> &nbsp; &nbsp;const reader = fs.createReadStream(file.path);<br> &nbsp; &nbsp;let filePath = path.join(__dirname, 'public\/upload\/') + `\/${file.name}`;<br> &nbsp; &nbsp;console.log(filePath)<br> &nbsp; &nbsp;\/\/ \u521b\u5efa\u53ef\u5199\u6d41<br> &nbsp; &nbsp;const upStream = fs.createWriteStream(filePath);<br> &nbsp; &nbsp;\/\/ \u53ef\u8bfb\u6d41\u901a\u8fc7\u7ba1\u9053\u5199\u5165\u53ef\u5199\u6d41<br> &nbsp; &nbsp;reader.pipe(upStream);<br> &nbsp; &nbsp;return ctx.body = \"\u4e0a\u4f20\u6210\u529f\uff01\";<br>})<br>module.exports = router<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>GET\u683c\u5f0f\u53c2\u6570\u63a5\u6536 POST\u683c\u5f0f\u63a5\u6536(\u652f\u6301JSON \\ form-data \\ x-www-form-urle [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-279","post","type-post","status-publish","format-standard","hentry","category-koa2"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/279","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=279"}],"version-history":[{"count":0,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/279\/revisions"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=279"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}