Ver código fonte

Node.js day2:文件上传

daxia 2 anos atrás
pai
commit
7816832e5f

+ 144 - 1
19_Node.js/Koa快速入门.md

@@ -95,5 +95,148 @@ router
 export default router;
 ```
 
-### 3.4 Get请求方法
+### 3.4 处理Get请求方法
+
+> 在Koa框架中,会自动对所有请求地址中的查询字符串进行转译操作,也就是说 自动将查询字符串 转换成 普通Object对象形式。处理完成后,会存储在`ctx.query`上。
+
+比如,我们需要实现分页获取数据时就会像这样:
+
+```js
+import Router from '@koa/router';
+const router = new Router();
+// 定义一些静态数据
+const users = [
+  {
+    id: 1,
+    name: '果果',
+    age: 18,
+  },
+  {
+    id: 2,
+    name: '静静',
+    age: 38,
+  },
+  {
+    id: 3,
+    name: '欣欣',
+    age: 18,
+  },
+  {
+    id: 4,
+    name: '诗诗',
+    age: 28,
+  },
+];
+
+router
+  // 获取所有用户数据
+  .get('/', (ctx) => {
+    //! 如果响应数据类型 赋值为 一个对象,那么koa会自动设置响应头content-type = application/json;chartset=utf-8
+    ctx.body = users;
+  })
+  // 分页获取数据
+  // 思考:1 是get还是post;get 2 需要给服务端 传递数据吗? 当前页码current and 每页条数count
+  .get('/page', (ctx) => {
+    // ? 如何获取用户传递过来的参数current以及count?
+    //! koa框架 会 自动处理所有请求的查询参数 -- 在处理路由时,可以直接通过ctx.query来获取所有的查询参数
+    // ctx.query 获取所有转译后查询参数对象
+    // console.log(ctx.query);
+    const { current, count } = ctx.query;
+
+    // 从总数据的数组中正确的截取出我们想要的那些数据
+    // 1 用数组的slice方法 进行 数据截取
+    // 1: [0,1) 2: [1, 2)  3: [2, 3)... m: [(m-1)*count, count * m )
+
+    ctx.body = users.slice((current - 1) * count, current * count);
+  });
+
+export default router;
+```
+
+### 3.5 处理Post请求方法
+
+> 在处理Post请求时,如果客户端传递的数据 是 通过请求体body实现的,那么默认Koa是无法处理的转换工作的。
+
+如果想要在所有路由处理响应之前,能够将Post请求体转换成普通对象就需要安装第三方的中间件。
+
+```bash
+npm install koa-body
+```
+
+接下来,需要在入口文件`index.js`写下如下代码:
+
+```js
+import Koa from 'koa';
+import serve from 'koa-static';
+import config from './app.config.js';
+import router from './router/index.mjs';
+import { koaBody } from 'koa-body';
+
+const app = new Koa();
+// 托管静态文件
+app.use(serve(config.public));
+// 一定要在路由之前先处理请求体的转换
+// 默认对 json and xml and urlencoded 进行转换
+app.use(koaBody());
+// 添加路由
+app.use(router.routes());
+// 在options请求下响应支持的请求方法
+app.use(router.allowedMethods());
+
+app.listen(config.port, config.host, () => {
+  console.info(`服务已启动,地址为:'http://${config.host}:${config.port}'.`);
+});
+```
+
+上述代码中:`app.use(koaBody())`就实现了对json以及urlencoded数据类型的body转换了,之后就可以通过`ctx.request.body`来获取转换后的请求体数据(类型为 普通JS对象)
+
+先这样,
+
+```js
+import Router from '@koa/router';
+
+const router = new Router();
+// 定义一些静态数据
+const users = [
+  {
+    id: 1,
+    name: '果果',
+    age: 18,
+  },
+  {
+    id: 2,
+    name: '静静',
+    age: 38,
+  },
+  {
+    id: 3,
+    name: '欣欣',
+    age: 18,
+  },
+  {
+    id: 4,
+    name: '诗诗',
+    age: 28,
+  },
+];
+
+let uid = 4; // 用来储存当前最新添加用户的id值
+
+router
+  // 获取所有用户数据
+  .get('/', (ctx) => {
+    //! 如果响应数据类型 赋值为 一个对象,那么koa会自动设置响应头content-type = application/json;chartset=utf-8
+    ctx.body = users;
+  })
+  // 添加新用户
+  .post('/insert', (ctx) => {
+    // console.log(ctx.request.body);
+    users.push({ ...ctx.request.body, id: ++uid });
+    ctx.body = '添加成功';
+  });
+
+export default router;
+```
+
+### 3.6 文件上传
 

+ 5 - 0
19_Node.js/day-3/code/koa-app/index.js

@@ -2,11 +2,16 @@ import Koa from 'koa';
 import serve from 'koa-static';
 import config from './app.config.js';
 import router from './router/index.mjs';
+import { koaBody } from 'koa-body';
 
 const app = new Koa();
 // 托管静态文件
 app.use(serve(config.public));
 
+// 一定要在路由之前先处理请求体的转换
+// 默认对 json and xml and urlencoded 进行转换
+app.use(koaBody());
+
 // 添加路由
 app.use(router.routes());
 // 在options请求下响应支持的请求方法

+ 337 - 0
19_Node.js/day-3/code/koa-app/package-lock.json

@@ -11,6 +11,7 @@
       "dependencies": {
         "@koa/router": "^12.0.0",
         "koa": "^2.14.2",
+        "koa-body": "^6.0.1",
         "koa-static": "^5.0.0"
       }
     },
@@ -28,6 +29,152 @@
         "node": ">= 12"
       }
     },
+    "node_modules/@types/accepts": {
+      "version": "1.3.5",
+      "resolved": "https://registry.npmmirror.com/@types/accepts/-/accepts-1.3.5.tgz",
+      "integrity": "sha512-jOdnI/3qTpHABjM5cx1Hc0sKsPoYCp+DP/GJRGtDlPd7fiV9oXGGIcjW/ZOxLIvjGz8MA+uMZI9metHlgqbgwQ==",
+      "dependencies": {
+        "@types/node": "*"
+      }
+    },
+    "node_modules/@types/body-parser": {
+      "version": "1.19.2",
+      "resolved": "https://registry.npmmirror.com/@types/body-parser/-/body-parser-1.19.2.tgz",
+      "integrity": "sha512-ALYone6pm6QmwZoAgeyNksccT9Q4AWZQ6PvfwR37GT6r6FWUPguq6sUmNGSMV2Wr761oQoBxwGGa6DR5o1DC9g==",
+      "dependencies": {
+        "@types/connect": "*",
+        "@types/node": "*"
+      }
+    },
+    "node_modules/@types/co-body": {
+      "version": "6.1.0",
+      "resolved": "https://registry.npmmirror.com/@types/co-body/-/co-body-6.1.0.tgz",
+      "integrity": "sha512-3e0q2jyDAnx/DSZi0z2H0yoZ2wt5yRDZ+P7ymcMObvq0ufWRT4tsajyO+Q1VwVWiv9PRR4W3YEjEzBjeZlhF+w==",
+      "dependencies": {
+        "@types/node": "*",
+        "@types/qs": "*"
+      }
+    },
+    "node_modules/@types/connect": {
+      "version": "3.4.35",
+      "resolved": "https://registry.npmmirror.com/@types/connect/-/connect-3.4.35.tgz",
+      "integrity": "sha512-cdeYyv4KWoEgpBISTxWvqYsVy444DOqehiF3fM3ne10AmJ62RSyNkUnxMJXHQWRQQX2eR94m5y1IZyDwBjV9FQ==",
+      "dependencies": {
+        "@types/node": "*"
+      }
+    },
+    "node_modules/@types/content-disposition": {
+      "version": "0.5.5",
+      "resolved": "https://registry.npmmirror.com/@types/content-disposition/-/content-disposition-0.5.5.tgz",
+      "integrity": "sha512-v6LCdKfK6BwcqMo+wYW05rLS12S0ZO0Fl4w1h4aaZMD7bqT3gVUns6FvLJKGZHQmYn3SX55JWGpziwJRwVgutA=="
+    },
+    "node_modules/@types/cookies": {
+      "version": "0.7.7",
+      "resolved": "https://registry.npmmirror.com/@types/cookies/-/cookies-0.7.7.tgz",
+      "integrity": "sha512-h7BcvPUogWbKCzBR2lY4oqaZbO3jXZksexYJVFvkrFeLgbZjQkU4x8pRq6eg2MHXQhY0McQdqmmsxRWlVAHooA==",
+      "dependencies": {
+        "@types/connect": "*",
+        "@types/express": "*",
+        "@types/keygrip": "*",
+        "@types/node": "*"
+      }
+    },
+    "node_modules/@types/express": {
+      "version": "4.17.17",
+      "resolved": "https://registry.npmmirror.com/@types/express/-/express-4.17.17.tgz",
+      "integrity": "sha512-Q4FmmuLGBG58btUnfS1c1r/NQdlp3DMfGDGig8WhfpA2YRUtEkxAjkZb0yvplJGYdF1fsQ81iMDcH24sSCNC/Q==",
+      "dependencies": {
+        "@types/body-parser": "*",
+        "@types/express-serve-static-core": "^4.17.33",
+        "@types/qs": "*",
+        "@types/serve-static": "*"
+      }
+    },
+    "node_modules/@types/express-serve-static-core": {
+      "version": "4.17.33",
+      "resolved": "https://registry.npmmirror.com/@types/express-serve-static-core/-/express-serve-static-core-4.17.33.tgz",
+      "integrity": "sha512-TPBqmR/HRYI3eC2E5hmiivIzv+bidAfXofM+sbonAGvyDhySGw9/PQZFt2BLOrjUUR++4eJVpx6KnLQK1Fk9tA==",
+      "dependencies": {
+        "@types/node": "*",
+        "@types/qs": "*",
+        "@types/range-parser": "*"
+      }
+    },
+    "node_modules/@types/formidable": {
+      "version": "2.0.5",
+      "resolved": "https://registry.npmmirror.com/@types/formidable/-/formidable-2.0.5.tgz",
+      "integrity": "sha512-uvMcdn/KK3maPOaVUAc3HEYbCEhjaGFwww4EsX6IJfWIJ1tzHtDHczuImH3GKdusPnAAmzB07St90uabZeCKPA==",
+      "dependencies": {
+        "@types/node": "*"
+      }
+    },
+    "node_modules/@types/http-assert": {
+      "version": "1.5.3",
+      "resolved": "https://registry.npmmirror.com/@types/http-assert/-/http-assert-1.5.3.tgz",
+      "integrity": "sha512-FyAOrDuQmBi8/or3ns4rwPno7/9tJTijVW6aQQjK02+kOQ8zmoNg2XJtAuQhvQcy1ASJq38wirX5//9J1EqoUA=="
+    },
+    "node_modules/@types/http-errors": {
+      "version": "2.0.1",
+      "resolved": "https://registry.npmmirror.com/@types/http-errors/-/http-errors-2.0.1.tgz",
+      "integrity": "sha512-/K3ds8TRAfBvi5vfjuz8y6+GiAYBZ0x4tXv1Av6CWBWn0IlADc+ZX9pMq7oU0fNQPnBwIZl3rmeLp6SBApbxSQ=="
+    },
+    "node_modules/@types/keygrip": {
+      "version": "1.0.2",
+      "resolved": "https://registry.npmmirror.com/@types/keygrip/-/keygrip-1.0.2.tgz",
+      "integrity": "sha512-GJhpTepz2udxGexqos8wgaBx4I/zWIDPh/KOGEwAqtuGDkOUJu5eFvwmdBX4AmB8Odsr+9pHCQqiAqDL/yKMKw=="
+    },
+    "node_modules/@types/koa": {
+      "version": "2.13.6",
+      "resolved": "https://registry.npmmirror.com/@types/koa/-/koa-2.13.6.tgz",
+      "integrity": "sha512-diYUfp/GqfWBAiwxHtYJ/FQYIXhlEhlyaU7lB/bWQrx4Il9lCET5UwpFy3StOAohfsxxvEQ11qIJgT1j2tfBvw==",
+      "dependencies": {
+        "@types/accepts": "*",
+        "@types/content-disposition": "*",
+        "@types/cookies": "*",
+        "@types/http-assert": "*",
+        "@types/http-errors": "*",
+        "@types/keygrip": "*",
+        "@types/koa-compose": "*",
+        "@types/node": "*"
+      }
+    },
+    "node_modules/@types/koa-compose": {
+      "version": "3.2.5",
+      "resolved": "https://registry.npmmirror.com/@types/koa-compose/-/koa-compose-3.2.5.tgz",
+      "integrity": "sha512-B8nG/OoE1ORZqCkBVsup/AKcvjdgoHnfi4pZMn5UwAPCbhk/96xyv284eBYW8JlQbQ7zDmnpFr68I/40mFoIBQ==",
+      "dependencies": {
+        "@types/koa": "*"
+      }
+    },
+    "node_modules/@types/mime": {
+      "version": "3.0.1",
+      "resolved": "https://registry.npmmirror.com/@types/mime/-/mime-3.0.1.tgz",
+      "integrity": "sha512-Y4XFY5VJAuw0FgAqPNd6NNoV44jbq9Bz2L7Rh/J6jLTiHBSBJa9fxqQIvkIld4GsoDOcCbvzOUAbLPsSKKg+uA=="
+    },
+    "node_modules/@types/node": {
+      "version": "18.15.11",
+      "resolved": "https://registry.npmmirror.com/@types/node/-/node-18.15.11.tgz",
+      "integrity": "sha512-E5Kwq2n4SbMzQOn6wnmBjuK9ouqlURrcZDVfbo9ftDDTFt3nk7ZKK4GMOzoYgnpQJKcxwQw+lGaBvvlMo0qN/Q=="
+    },
+    "node_modules/@types/qs": {
+      "version": "6.9.7",
+      "resolved": "https://registry.npmmirror.com/@types/qs/-/qs-6.9.7.tgz",
+      "integrity": "sha512-FGa1F62FT09qcrueBA6qYTrJPVDzah9a+493+o2PCXsesWHIn27G98TsSMs3WPNbZIEj4+VJf6saSFpvD+3Zsw=="
+    },
+    "node_modules/@types/range-parser": {
+      "version": "1.2.4",
+      "resolved": "https://registry.npmmirror.com/@types/range-parser/-/range-parser-1.2.4.tgz",
+      "integrity": "sha512-EEhsLsD6UsDM1yFhAvy0Cjr6VwmpMWqFBCb9w07wVugF7w9nfajxLuVmngTIpgS6svCnm6Vaw+MZhoDCKnOfsw=="
+    },
+    "node_modules/@types/serve-static": {
+      "version": "1.15.1",
+      "resolved": "https://registry.npmmirror.com/@types/serve-static/-/serve-static-1.15.1.tgz",
+      "integrity": "sha512-NUo5XNiAdULrJENtJXZZ3fHtfMolzZwczzBbnAeBbqBwG+LaG6YaJtuwzwGSQZ2wsCrxjEhNNjAkKigy3n8teQ==",
+      "dependencies": {
+        "@types/mime": "*",
+        "@types/node": "*"
+      }
+    },
     "node_modules/accepts": {
       "version": "1.3.8",
       "resolved": "https://registry.npmmirror.com/accepts/-/accepts-1.3.8.tgz",
@@ -40,6 +187,19 @@
         "node": ">= 0.6"
       }
     },
+    "node_modules/asap": {
+      "version": "2.0.6",
+      "resolved": "https://registry.npmmirror.com/asap/-/asap-2.0.6.tgz",
+      "integrity": "sha512-BSHWgDSAiKs50o2Re8ppvp3seVHXSRM44cdSsT9FfNEUUZLOGWVCsiWaRPWM1Znn+mqZ1OfVZ3z3DWEzSp7hRA=="
+    },
+    "node_modules/bytes": {
+      "version": "3.1.2",
+      "resolved": "https://registry.npmmirror.com/bytes/-/bytes-3.1.2.tgz",
+      "integrity": "sha512-/Nf7TyzTx6S3yRJObOAV7956r8cr2+Oj8AC5dt8wSP3BQAoeX58NoHyCU8P8zGkNXStjTSi6fzO6F0pBdcYbEg==",
+      "engines": {
+        "node": ">= 0.8"
+      }
+    },
     "node_modules/cache-content-type": {
       "version": "1.0.1",
       "resolved": "https://registry.npmmirror.com/cache-content-type/-/cache-content-type-1.0.1.tgz",
@@ -52,6 +212,15 @@
         "node": ">= 6.0.0"
       }
     },
+    "node_modules/call-bind": {
+      "version": "1.0.2",
+      "resolved": "https://registry.npmmirror.com/call-bind/-/call-bind-1.0.2.tgz",
+      "integrity": "sha512-7O+FbCihrB5WGbFYesctwmTKae6rOiIzmz1icreWJ+0aA7LJfuqhEso2T9ncpcFtzMQtzXf2QGGueWJGTYsqrA==",
+      "dependencies": {
+        "function-bind": "^1.1.1",
+        "get-intrinsic": "^1.0.2"
+      }
+    },
     "node_modules/co": {
       "version": "4.6.0",
       "resolved": "https://registry.npmmirror.com/co/-/co-4.6.0.tgz",
@@ -61,6 +230,17 @@
         "node": ">= 0.12.0"
       }
     },
+    "node_modules/co-body": {
+      "version": "6.1.0",
+      "resolved": "https://registry.npmmirror.com/co-body/-/co-body-6.1.0.tgz",
+      "integrity": "sha512-m7pOT6CdLN7FuXUcpuz/8lfQ/L77x8SchHCF4G0RBTJO20Wzmhn5Sp4/5WsKy8OSpifBSUrmg83qEqaDHdyFuQ==",
+      "dependencies": {
+        "inflation": "^2.0.0",
+        "qs": "^6.5.2",
+        "raw-body": "^2.3.3",
+        "type-is": "^1.6.16"
+      }
+    },
     "node_modules/content-disposition": {
       "version": "0.5.4",
       "resolved": "https://registry.npmmirror.com/content-disposition/-/content-disposition-0.5.4.tgz",
@@ -135,6 +315,15 @@
         "npm": "1.2.8000 || >= 1.4.16"
       }
     },
+    "node_modules/dezalgo": {
+      "version": "1.0.4",
+      "resolved": "https://registry.npmmirror.com/dezalgo/-/dezalgo-1.0.4.tgz",
+      "integrity": "sha512-rXSP0bf+5n0Qonsb+SVVfNfIsimO4HEtmnIpPHY8Q1UCzKlQrDMfdobr8nJOOsRgWCyMRqeSBQzmWUMq7zvVig==",
+      "dependencies": {
+        "asap": "^2.0.0",
+        "wrappy": "1"
+      }
+    },
     "node_modules/ee-first": {
       "version": "1.1.1",
       "resolved": "https://registry.npmmirror.com/ee-first/-/ee-first-1.1.1.tgz",
@@ -153,6 +342,17 @@
       "resolved": "https://registry.npmmirror.com/escape-html/-/escape-html-1.0.3.tgz",
       "integrity": "sha512-NiSupZ4OeuGwr68lGIeym/ksIZMJodUGOSCZ/FSnTxcrekbvqrgdUxlJOMpijaKZVjAJrWrGs/6Jy8OMuyj9ow=="
     },
+    "node_modules/formidable": {
+      "version": "2.1.1",
+      "resolved": "https://registry.npmmirror.com/formidable/-/formidable-2.1.1.tgz",
+      "integrity": "sha512-0EcS9wCFEzLvfiks7omJ+SiYJAiD+TzK4Pcw1UlUoGnhUxDcMKjt0P7x8wEb0u6OHu8Nb98WG3nxtlF5C7bvUQ==",
+      "dependencies": {
+        "dezalgo": "^1.0.4",
+        "hexoid": "^1.0.0",
+        "once": "^1.4.0",
+        "qs": "^6.11.0"
+      }
+    },
     "node_modules/fresh": {
       "version": "0.5.2",
       "resolved": "https://registry.npmmirror.com/fresh/-/fresh-0.5.2.tgz",
@@ -161,6 +361,32 @@
         "node": ">= 0.6"
       }
     },
+    "node_modules/function-bind": {
+      "version": "1.1.1",
+      "resolved": "https://registry.npmmirror.com/function-bind/-/function-bind-1.1.1.tgz",
+      "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A=="
+    },
+    "node_modules/get-intrinsic": {
+      "version": "1.2.0",
+      "resolved": "https://registry.npmmirror.com/get-intrinsic/-/get-intrinsic-1.2.0.tgz",
+      "integrity": "sha512-L049y6nFOuom5wGyRc3/gdTLO94dySVKRACj1RmJZBQXlbTMhtNIgkWkUHq+jYmZvKf14EW1EoJnnjbmoHij0Q==",
+      "dependencies": {
+        "function-bind": "^1.1.1",
+        "has": "^1.0.3",
+        "has-symbols": "^1.0.3"
+      }
+    },
+    "node_modules/has": {
+      "version": "1.0.3",
+      "resolved": "https://registry.npmmirror.com/has/-/has-1.0.3.tgz",
+      "integrity": "sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw==",
+      "dependencies": {
+        "function-bind": "^1.1.1"
+      },
+      "engines": {
+        "node": ">= 0.4.0"
+      }
+    },
     "node_modules/has-symbols": {
       "version": "1.0.3",
       "resolved": "https://registry.npmmirror.com/has-symbols/-/has-symbols-1.0.3.tgz",
@@ -180,6 +406,14 @@
         "node": ">= 0.4"
       }
     },
+    "node_modules/hexoid": {
+      "version": "1.0.0",
+      "resolved": "https://registry.npmmirror.com/hexoid/-/hexoid-1.0.0.tgz",
+      "integrity": "sha512-QFLV0taWQOZtvIRIAdBChesmogZrtuXvVWsFHZTk2SU+anspqZ2vMnoLg7IE1+Uk16N19APic1BuF8bC8c2m5g==",
+      "engines": {
+        "node": ">=8"
+      }
+    },
     "node_modules/http-assert": {
       "version": "1.5.0",
       "resolved": "https://registry.npmmirror.com/http-assert/-/http-assert-1.5.0.tgz",
@@ -238,6 +472,25 @@
         "node": ">= 0.8"
       }
     },
+    "node_modules/iconv-lite": {
+      "version": "0.4.24",
+      "resolved": "https://registry.npmmirror.com/iconv-lite/-/iconv-lite-0.4.24.tgz",
+      "integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==",
+      "dependencies": {
+        "safer-buffer": ">= 2.1.2 < 3"
+      },
+      "engines": {
+        "node": ">=0.10.0"
+      }
+    },
+    "node_modules/inflation": {
+      "version": "2.0.0",
+      "resolved": "https://registry.npmmirror.com/inflation/-/inflation-2.0.0.tgz",
+      "integrity": "sha512-m3xv4hJYR2oXw4o4Y5l6P5P16WYmazYof+el6Al3f+YlggGj6qT9kImBAnzDelRALnP5d3h4jGBPKzYCizjZZw==",
+      "engines": {
+        "node": ">= 0.8.0"
+      }
+    },
     "node_modules/inherits": {
       "version": "2.0.4",
       "resolved": "https://registry.npmmirror.com/inherits/-/inherits-2.0.4.tgz",
@@ -298,6 +551,19 @@
         "node": "^4.8.4 || ^6.10.1 || ^7.10.1 || >= 8.1.4"
       }
     },
+    "node_modules/koa-body": {
+      "version": "6.0.1",
+      "resolved": "https://registry.npmmirror.com/koa-body/-/koa-body-6.0.1.tgz",
+      "integrity": "sha512-M8ZvMD8r+kPHy28aWP9VxL7kY8oPWA+C7ZgCljrCMeaU7uX6wsIQgDHskyrAr9sw+jqnIXyv4Mlxri5R4InIJg==",
+      "dependencies": {
+        "@types/co-body": "^6.1.0",
+        "@types/formidable": "^2.0.5",
+        "@types/koa": "^2.13.5",
+        "co-body": "^6.1.0",
+        "formidable": "^2.0.1",
+        "zod": "^3.19.1"
+      }
+    },
     "node_modules/koa-compose": {
       "version": "4.1.0",
       "resolved": "https://registry.npmmirror.com/koa-compose/-/koa-compose-4.1.0.tgz",
@@ -458,6 +724,11 @@
         "node": ">= 0.6"
       }
     },
+    "node_modules/object-inspect": {
+      "version": "1.12.3",
+      "resolved": "https://registry.npmmirror.com/object-inspect/-/object-inspect-1.12.3.tgz",
+      "integrity": "sha512-geUvdk7c+eizMNUDkRpW1wJwgfOiOeHbxBR/hLXK1aT6zmVSO0jsQcs7fj6MGw89jC/cjGfLcNOrtMYtGqm81g=="
+    },
     "node_modules/on-finished": {
       "version": "2.4.1",
       "resolved": "https://registry.npmmirror.com/on-finished/-/on-finished-2.4.1.tgz",
@@ -469,6 +740,14 @@
         "node": ">= 0.8"
       }
     },
+    "node_modules/once": {
+      "version": "1.4.0",
+      "resolved": "https://registry.npmmirror.com/once/-/once-1.4.0.tgz",
+      "integrity": "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==",
+      "dependencies": {
+        "wrappy": "1"
+      }
+    },
     "node_modules/only": {
       "version": "0.0.2",
       "resolved": "https://registry.npmmirror.com/only/-/only-0.0.2.tgz",
@@ -495,6 +774,31 @@
       "resolved": "https://registry.npmmirror.com/path-to-regexp/-/path-to-regexp-6.2.1.tgz",
       "integrity": "sha512-JLyh7xT1kizaEvcaXOQwOc2/Yhw6KZOvPf1S8401UyLk86CU79LN3vl7ztXGm/pZ+YjoyAJ4rxmHwbkBXJX+yw=="
     },
+    "node_modules/qs": {
+      "version": "6.11.1",
+      "resolved": "https://registry.npmmirror.com/qs/-/qs-6.11.1.tgz",
+      "integrity": "sha512-0wsrzgTz/kAVIeuxSjnpGC56rzYtr6JT/2BwEvMaPhFIoYa1aGO8LbzuU1R0uUYQkLpWBTOj0l/CLAJB64J6nQ==",
+      "dependencies": {
+        "side-channel": "^1.0.4"
+      },
+      "engines": {
+        "node": ">=0.6"
+      }
+    },
+    "node_modules/raw-body": {
+      "version": "2.5.2",
+      "resolved": "https://registry.npmmirror.com/raw-body/-/raw-body-2.5.2.tgz",
+      "integrity": "sha512-8zGqypfENjCIqGhgXToC8aB2r7YrBX+AQAfIPs/Mlk+BtPTztOvTS01NRW/3Eh60J+a48lt8qsCzirQ6loCVfA==",
+      "dependencies": {
+        "bytes": "3.1.2",
+        "http-errors": "2.0.0",
+        "iconv-lite": "0.4.24",
+        "unpipe": "1.0.0"
+      },
+      "engines": {
+        "node": ">= 0.8"
+      }
+    },
     "node_modules/resolve-path": {
       "version": "1.4.0",
       "resolved": "https://registry.npmmirror.com/resolve-path/-/resolve-path-1.4.0.tgz",
@@ -552,11 +856,26 @@
       "resolved": "https://registry.npmmirror.com/safe-buffer/-/safe-buffer-5.2.1.tgz",
       "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ=="
     },
+    "node_modules/safer-buffer": {
+      "version": "2.1.2",
+      "resolved": "https://registry.npmmirror.com/safer-buffer/-/safer-buffer-2.1.2.tgz",
+      "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg=="
+    },
     "node_modules/setprototypeof": {
       "version": "1.2.0",
       "resolved": "https://registry.npmmirror.com/setprototypeof/-/setprototypeof-1.2.0.tgz",
       "integrity": "sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw=="
     },
+    "node_modules/side-channel": {
+      "version": "1.0.4",
+      "resolved": "https://registry.npmmirror.com/side-channel/-/side-channel-1.0.4.tgz",
+      "integrity": "sha512-q5XPytqFEIKHkGdiMIrY10mvLRvnQh42/+GoBlFW3b2LXLE2xxJpZFdm94we0BaoV3RwJyGqg5wS7epxTv0Zvw==",
+      "dependencies": {
+        "call-bind": "^1.0.0",
+        "get-intrinsic": "^1.0.2",
+        "object-inspect": "^1.9.0"
+      }
+    },
     "node_modules/statuses": {
       "version": "2.0.1",
       "resolved": "https://registry.npmmirror.com/statuses/-/statuses-2.0.1.tgz",
@@ -593,6 +912,14 @@
         "node": ">= 0.6"
       }
     },
+    "node_modules/unpipe": {
+      "version": "1.0.0",
+      "resolved": "https://registry.npmmirror.com/unpipe/-/unpipe-1.0.0.tgz",
+      "integrity": "sha512-pjy2bYhSsufwWlKwPc+l3cN7+wuJlK6uz0YdJEOlQDbl6jo/YlPi4mb8agUkVC8BF7V8NuzeyPNqRksA3hztKQ==",
+      "engines": {
+        "node": ">= 0.8"
+      }
+    },
     "node_modules/vary": {
       "version": "1.1.2",
       "resolved": "https://registry.npmmirror.com/vary/-/vary-1.1.2.tgz",
@@ -601,6 +928,11 @@
         "node": ">= 0.8"
       }
     },
+    "node_modules/wrappy": {
+      "version": "1.0.2",
+      "resolved": "https://registry.npmmirror.com/wrappy/-/wrappy-1.0.2.tgz",
+      "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ=="
+    },
     "node_modules/ylru": {
       "version": "1.3.2",
       "resolved": "https://registry.npmmirror.com/ylru/-/ylru-1.3.2.tgz",
@@ -608,6 +940,11 @@
       "engines": {
         "node": ">= 4.0.0"
       }
+    },
+    "node_modules/zod": {
+      "version": "3.21.4",
+      "resolved": "https://registry.npmmirror.com/zod/-/zod-3.21.4.tgz",
+      "integrity": "sha512-m46AKbrzKVzOzs/DZgVnG5H55N1sv1M8qZU3A8RIKbs3mrACDNeIOeilDymVb2HdmP8uwshOCF4uJ8uM9rCqJw=="
     }
   }
 }

+ 1 - 0
19_Node.js/day-3/code/koa-app/package.json

@@ -14,6 +14,7 @@
   "dependencies": {
     "@koa/router": "^12.0.0",
     "koa": "^2.14.2",
+    "koa-body": "^6.0.1",
     "koa-static": "^5.0.0"
   }
 }

BIN
19_Node.js/day-3/code/koa-app/public/30aad8c6bedabd044d8d0d800.jpeg


BIN
19_Node.js/day-3/code/koa-app/public/90eb68f0d7cc4961332312800.jpeg


BIN
19_Node.js/day-3/code/koa-app/public/d0d4f514daf50daf7a6f66e00.jpeg


+ 26 - 1
19_Node.js/day-3/code/koa-app/router/index.mjs

@@ -1,4 +1,5 @@
 import Router from '@koa/router';
+import { koaBody } from 'koa-body';
 
 const router = new Router();
 
@@ -26,6 +27,8 @@ const users = [
   },
 ];
 
+let uid = 4; // 用来储存当前最新添加用户的id值
+
 router
   // 获取所有用户数据
   .get('/', (ctx) => {
@@ -46,6 +49,28 @@ router
     // 1: [0,1) 2: [1, 2)  3: [2, 3)... m: [(m-1)*count, count * m )
 
     ctx.body = users.slice((current - 1) * count, current * count);
-  });
+  })
+  // 添加新用户
+  .post('/insert', (ctx) => {
+    // console.log(ctx.request.body);
+    users.push({ ...ctx.request.body, id: ++uid });
+    ctx.body = '添加成功';
+  })
+  // 上传文件
+  .put(
+    '/upload',
+    koaBody({
+      multipart: true,
+      formidable: {
+        uploadDir: './public',
+        keepExtensions: true,
+      },
+    }),
+    (ctx) => {
+      // 获取待上传的文件
+      console.log(ctx.request.files);
+      ctx.body = `/${ctx.request.files.file.newFilename}`;
+    }
+  );
 
 export default router;

BIN
19_Node.js/day-3/note/请求体常见编码格式.png