• 82 # koa-bodyparser 中间件的使用以及实现


    准备工作

    安装依赖

    npm init -y
    npm i koa
    
    • 1
    • 2

    在这里插入图片描述

    koa 文档:https://koajs.cn/#

    koa 中不能用回调的方式来实现,因为 async 函数执行的时候不会等待回调完成

    app.use(async (ctx, next) => {
        console.log(ctx.path, ctx.method);
        if (ctx.path == "/login" && ctx.method === "POST") {
            const arr = [];
            ctx.req.on("data", function (chunk) {
                arr.push(chunk);
            });
            ctx.req.on("end", function () {
                const result = Buffer.concat(arr).toString();
                console.log("result---->", result);
                ctx.body = result;
            });
        } else {
            next();
        }
    });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    koa 中所有的异步都必须是 promise,只有 promise 才有等待效果,必须所有的 next 方法前需要有 await、return 否则没有等待效果

    app.use(async (ctx, next) => {
        console.log(ctx.path, ctx.method);
        if (ctx.path == "/login" && ctx.method === "POST") {
            await new Promise((resolve, reject) => {
                const arr = [];
                ctx.req.on("data", function (chunk) {
                    arr.push(chunk);
                });
                ctx.req.on("end", function () {
                    const result = Buffer.concat(arr).toString();
                    console.log("result---->", result);
                    ctx.body = result;
                    resolve();
                });
            });
        } else {
            await next();
        }
    });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    实现一个表单提交功能 server.js

    const Koa = require("koa");
    
    const app = new Koa();
    
    app.use((ctx, next) => {
        // 路径是 /login get 方式
        // ctx 包含了 request response req res
        console.log(ctx.path, ctx.method);
        if (ctx.path == "/login" && ctx.method === "GET") {
            ctx.body = `
                
    用户名:
    密码:
    `
    ; } else { return next(); } }); app.use(async (ctx, next) => { console.log(ctx.path, ctx.method); if (ctx.path == "/login" && ctx.method === "POST") { await new Promise((resolve, reject) => { const arr = []; ctx.req.on("data", function (chunk) { arr.push(chunk); }); ctx.req.on("end", function () { const result = Buffer.concat(arr).toString(); console.log("result---->", result); ctx.body = result; resolve(); }); }); } else { await next(); } }); app.on("error", function (err) { console.log("error----->", err); }); app.listen(3000);
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    启动服务,访问 http://localhost:3000/login

    nodemon server.js
    
    • 1

    在这里插入图片描述

    输入账号密码,点击提交

    在这里插入图片描述

    koa-bodyparser

    下面使用 koa-bodyparser 简化逻辑,安装 koa-bodyparserhttps://www.npmjs.com/package/koa-bodyparser

    npm i koa-bodyparser
    
    • 1

    用法:

    const Koa = require('koa');
    const bodyParser = require('koa-bodyparser');
    
    const app = new Koa();
    app.use(bodyParser());
    
    app.use(async ctx => {
      // the parsed body will store in ctx.request.body
      // if nothing was parsed, body will be an empty object {}
      ctx.body = ctx.request.body;
    });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    业务里添加逻辑

    const Koa = require("koa");
    const bodyParser = require("koa-bodyparser");
    const app = new Koa();
    app.use(bodyParser());
    
    app.use((ctx, next) => {
        // 路径是 /login get 方式
        // ctx 包含了 request response req res
        console.log(ctx.path, ctx.method);
        if (ctx.path == "/login" && ctx.method === "GET") {
            ctx.body = `
                
    用户名:
    密码:
    `
    ; } else { return next(); } }); app.use(async (ctx, next) => { console.log(ctx.path, ctx.method); if (ctx.path == "/login" && ctx.method === "POST") { ctx.body = ctx.request.body; } else { await next(); } }); app.on("error", function (err) { console.log("error----->", err); }); app.listen(3000);
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    效果也是一样的

    下面自己实现 koa-bodyparser

    const querystring = require("querystring");
    console.log("使用的是 kaimo-koa-bodyparser 中间件");
    // 中间件的功能可以扩展属性、方法
    module.exports = function () {
        return async (ctx, next) => {
            await new Promise((resolve, reject) => {
                const arr = [];
                ctx.req.on("data", function (chunk) {
                    arr.push(chunk);
                });
                ctx.req.on("end", function () {
                    if (ctx.get("content-type") === "application/x-www-form-urlencoded") {
                        const result = Buffer.concat(arr).toString();
                        console.log("kaimo-koa-bodyparser-result---->", result);
                        ctx.request.body = querystring.parse(result);
                    }
                    resolve();
                });
            });
            await next(); // 完成后需要继续向下执行
        };
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    将业务代码的引用自己实现的

    // 使用自己实现的 koa-bodyparser
    const bodyParser = require("./kaimo-koa-bodyparser");
    
    • 1
    • 2

    启动服务,效果一样:

    在这里插入图片描述

  • 相关阅读:
    深度神经网络预测模型,神经网络预测未来数据
    [ACNOI2022]做过也不会
    SQL注入——预编译CASE注入
    大带宽服务器的作用有哪些?
    JVM 内存模型概述
    【小记】二八十十六,进制团团转
    java毕业设计网站SpringBoot美容院预约管理系统
    深入理解指针(c语言)
    在Windows模拟器中使用LVGL8.3
    iphone视频照片恢复
  • 原文地址:https://blog.csdn.net/kaimo313/article/details/132812456