nodejs koa框架使用

1: KOA 是express 打造的下一代web 开发框架提供更小更强的的核心功能,通过Promise 、async/await 进行异步编程,koa 可以不使用回调,解决了回调地狱的问题
blueBird 是nodejs 最出名的Primise 实现,除了实现标准的promise 之外,还提供了包装方法,可以快速得奖回调包装微Promise

2:bluebird 的使用
npm install bluebird -save
方法签名: bluebird.promisifyAll(target,options);

target 表示需要包装的对象,如果target是普通对象,包装后api 只会被当前对象持有,如果是原型对象,则会被所有的实例持有
options:
suffix :后缀,默认是Async
mutiArgs : 是否允许多个回调参数,默认是false,为true时,bluebird 将所有参数传入一个数组,promise.then() 接受该数组,从而可以得到多个参数
编程案例:

const bluebird = require('bluebird');
const fs = require('fs');bluebird.promisifyAll(fs);//回调
fs.readFile('文件路径',{encoding:'utf8'},(err,data)=>{if(err){console.error(err);return;}console.log(data);
});//Promise
fs.readFileAsync('文件路径',{encoding:'utf8'},(err,data)=>{console.log(data);
}).catch((err)=>{console.error(err);
});

编写第一个koa 程序:

//导入koa 模块
const Koa = require('koa');
//实例化应用
const app = new Koa();//中间件
app.use(async (ctx)=>{ctx.body = 'hello world';
});app.listen(8080,()=>{console.log('start up 8080');
})

//Context 成为koa 的文件上下文,包含了对象的请求、响应、context 可以理解为一个容器
//常用的属性和方法
//ctx.request koa的请求对象一般不直接使用,通过别名来访问
//ctx.response koa的响应对象一般不直接使用,通过别名来访问
//ctx.state 自定义数据存储,比如中间件需要往请种种挂在变量,就可以存放在
//state 后续中间件可以读取
//ctx.throw() 抛出http 异常
//ctx.headers 请求报头,ctx.request.headers 的别名
//ctx.method 请求方法,ctx.request.method 的别名
//ctx.url 请求连接,ctx.request.url 的别名
//ctx.path 请求路径,ctx.request.path 的别名
//ctx.query 请求参数,ctx.request.query 的别名,解析后的get请求参数对象
//ctx.host 请求域名地址,ctx.request.host 的别名
//ctx.ip 请求客户端ip,ctx.request.ip 的别名
//ctx.ips 请求ip列表,ctx.request.ips 的别名 ,反向代理环境下IP列表
//ctx.get() 读取请求报头,ctx.request.get 的别名
//ctx.body 响应内容,支持字符串,数组,对象,buffer 等,,ctx.response.get 的别名
//ctx.type 响应体类型,ctx.response.type 的别名
//ctx.redirect()重定向 ctx.response.redirect 的别名
//ctx.set() 设置相应报头 ,ctx.response.set 的别名

代码验证:

//导入koa 模块
const { method } = require('bluebird');
const Koa = require('koa');
//实例化应用
const app = new Koa();app.proxy = true;app.use(async (ctx)=>{ctx.set('x-version','1.0');ctx.body = {method: ctx.method,ip: ctx.ip,path: ctx.path,url: ctx.url,query: ctx.query,headers: ctx.headers} 
});app.listen(8080,()=>{console.log('start up 8080');
})

执行结果:
http://localhost:8080/?name=%E5%88%98%E4%BA%A6%E8%8F%B2

{"method":"GET","ip":"::ffff:127.0.0.1","path":"/","url":"/?name=%E5%88%98%E4%BA%A6%E8%8F%B2","query":{"name":"刘亦菲"},"headers":{"host":"localhost:8080","connection":"keep-alive","sec-ch-ua":"\"Not;A=Brand\";v=\"99\", \"Google Chrome\";v=\"139\", \"Chromium\";v=\"139\"","sec-ch-ua-mobile":"?0","sec-ch-ua-platform":"\"Windows\"","upgrade-insecure-requests":"1","user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36","accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7","sec-fetch-site":"none","sec-fetch-mode":"navigate","sec-fetch-user":"?1","sec-fetch-dest":"document","accept-encoding":"gzip, deflate, br, zstd","accept-language":"zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7"}}

Cookie 的设置和读取

//导入koa 模块
const { method } = require('bluebird');
const Koa = require('koa');
//实例化应用
const app = new Koa();app.keys = ['13wweeiioo'];
//cookie 设置和读取
app.use(async (ctx)=>{ctx.cookies.set('logined',1,{signed: true,httpOnly: true,maxAge: 3600 * 24 * 1000 //有效期一天});ctx.body = "ok la";
});app.listen(8080,()=>{console.log('start up 8080');
})

中间件
与expres 的中间件类似,Koa 的中间建也你能访问请求对象响应对象和next 函数通常用来执行以下任务:
执行逻辑
更改请求和响应
结束请求-相应周期
调用下一个中间建
错误处理

koa 使用async /await 来进行异步编程,不需要执行回调函数,直接对ctx.body 赋值就行
Koa 的中间件是一个表中的异步函数,签名如下:
async function middleware(ctx,next);
ctx 是上下文
next 下一个中间件
运行完代码逻辑,将需要传递的数据挂在到ctx.state ,并且调用 await.next() 才能将请求转发给下一个中间件
(洋葱卷模型)
代码案例:

const Koa = require('koa');
//实例化应用
const app = new Koa();async function mid1(ctx,next){console.log('mid1 begin');await next();console.log('mid1 end');
}async function mid2(ctx,next){console.log('mid2 begin');await next();console.log('mid2 end');
}app.use(mid1);
app.use(mid2);app.use(async (ctx)=>{console.log('request route!');ctx.body = 'Hello world';
});app.listen(8080,()=>{console.log('start up 8080');
})

执行结果:
mid1 begin
mid2 begin
request route!
mid2 end
mid1 end

日志中间件

const Koa = require('koa');
//实例化应用
const app = new Koa();async function logger(ctx,next) {const start = Date.now();await next();console.log(`${ctx.method} ${ctx.path} costTime:${Date.now() - start}ms`); 
}app.use(logger); //注册日志中间件
app.use(async (ctx)=> {ctx.body = 'Hello World';
});app.listen(8080,()=>{console.log('start up 8080');
})

cookie 解析中间件:

const Koa = require('koa');
//实例化应用
const app = new Koa();//定义cookie中间件
async function cookieParser(ctx,next) {const headerCookie = ctx.headers.cookie;ctx.state.cookies = {};if(headerCookie){const cookies = headerCookie.split(";");cookies.forEach(cookie => {const parts = cookie.split('=');ctx.state.cookies[parts[0] = parts[1]];//挂在 ctx.state.cookies});await next();}
}//注册中间件
app.use(cookieParser);//使用中间建
app.use(async (ctx)=>{ctx.body = ctx.state.cookies;
});//监听服务启动
app.listen(8000,()=>{console.log('start up 8000');
})

KOA 路由:
安装路由模块:
npm install koa-router -save
C:\Users\Lei.Wang170\Desktop\koa-01>npm install koa-router -save 这种写法已经不适用了
npm warn deprecated koa-router@14.0.0: Please use @koa/router instead, starting from v9!

使用新的安装命令:
C:\Users\Lei.Wang170\Desktop\koa-01>npm install @koa/router -save
added 1 package in 922ms
1 package is looking for funding
run npm fund for details
路由案例:

const Koa = require('koa');
const Router = require('koa-router');
//实例化应用
const app = new Koa();const router = new Router();//路由定义
router.get('/',async (ctx)=>{ctx.body ='hello world';
});router.get('/user',async (ctx)=>{ctx.body ='hello user';
});//挂载路由
app.use(router.routes());
app.use(router.allowedMethods());app.listen(8000,()=>{console.log('start up 8000');
})

错误处理:

const Koa = require('koa');
//实例化应用
const app = new Koa();//自定义中间件
async function errorHandler(ctx,next) {try {await next();} catch (error) {ctx.status = error.status || 500;ctx.body = `System Error: ${error.message}`;}
}
app.use(errorHandler);  //挂在中间件不需要括号,挂在路由需要括号()//抛出异常
app.use((ctx)=>{ctx.throw(403,'Forbidden');
});app.listen(8000,()=>{console.log('start up 8000');
})

注意错误处理器一定在在使用之前被挂载上,否则不成功

多个错误处理器的场景:
同时需要响应错误给调用端,并且要记录日志

const Koa = require('koa');
//实例化应用
const app = new Koa();//自定义中间件
async function errorHandler(ctx,next) {try {await next();} catch (error) {ctx.status = error.status || 500;ctx.body = `System Error: ${error.message}`;}
}//错误日志记录中间件
async function loggerHandler(ctx,next) {try {await next();} catch (error) {console.log(`error: ${ctx.path} ${ctx.ip}`);throw error;}
}app.use(errorHandler);  //挂在中间件不需要括号,挂在路由需要括号()
app.use(loggerHandler); 
//抛出异常
app.use((ctx)=>{ctx.throw(403,'Forbidden');
});app.listen(8010,()=>{console.log('start up 8010');
})

执行结果
在这里插入图片描述
在这里插入图片描述

路由级别的中间件

const Koa = require('koa');
const Router = require('koa-router');
const router = new Router();
//实例化应用
const app = new Koa();
//错误日志记录中间件
async function loggerHandler(ctx,next) {console.log(`error: ${ctx.path} ${ctx.ip}`);await next();      
}router.get('/',loggerHandler,async (ctx)=>{ctx.body ='hello world';
});app.listen(8010,()=>{console.log('start up 8010');
})

模块化路由:
student.js
const Router = require(‘koa-router’);
//定义带有前缀的路由
const router = new Router({ prefix: ‘/student’ });
//路由定义
router.get(‘/name’, (ctx)=>{
ctx.body =‘student’;
});
module.exports = router;

teacher.js
const Router = require(‘koa-router’);
//定义带有前缀的路由
const router = new Router({ prefix: ‘/teacher’ });
//路由定义
router.get(‘/id’, (ctx)=>{
ctx.body =‘teacher’;
});
module.exports = router;

koa.js
const Koa = require(‘koa’);
//实例化应用
const app = new Koa();

const stuRouter = require(‘./routes/student’);
const teaRouter = require(‘./routes/teacher’);

app.use(stuRouter.routes()).use(stuRouter.allowedMethods());
app.use(teaRouter.routes()).use(teaRouter.allowedMethods());

app.listen(8010,()=>{
console.log(‘start up 8010’);
})

koa-ejs 模板渲染:
npm install koa-ejs --save

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/bicheng/94260.shtml
繁体地址,请注明出处:http://hk.pswp.cn/bicheng/94260.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

2025年图像处理与光学国际会议(ICIPO 2025)

2025年图像处理与光学国际会议(ICIPO 2025) 2025 International Conference on Image Processing and Optics一、大会信息会议简称:ICIPO 2025 大会地点:中国北京 审稿通知:投稿后2-3日内通知 投稿邮箱:iac…

Kubernetes 构建高可用、高性能 Redis 集群

k8s下搭建Redis高可用1. 部署redis服务创建ConfigMap创建 Redis创建 k8s 集群外部2. 创建 Redis 集群自动创建 redis 集群手动创建 redis 集群验证集群状态3. 集群功能测试压力测试故障切换测试4. 安装管理客户端编辑资源清单部署 RedisInsight控制台初始化控制台概览实战环境使…

文件IO的基础操作

Java针对文件进行的操作:文件系统操作,File类(file类指定的路径,可以是一个不存在的文件)文件内容操作 : 流对象分为两类(1)字节流 以字节为基本的读写单位的 二进制文件 InputStream OutputStream(2)字符流 以字符为基本的读写单位的 …

【模版匹配】基于深度学习

基于深度学习的模版匹配 概述 本报告整理了2024-2025年最新的、可直接使用的模板匹配相关论文、方法和开源代码实现。所有方法都提供了完整的代码实现和预训练模型,可以直接应用到实际项目中。 一、轻量级现代模板匹配框架 1.1 UMatcher - 4M参数的紧凑型模板匹…

CMake进阶:Ninja环境搭建与加速项目构建

目录 1.引入Ninja的原因 2.Ninja 环境搭建(跨平台) 2.1.Linux系统安装 2.2.macOS 系统 2.3.Windows 系统 2.4.源码编译安装(通用方案) 3.Ninja 与构建系统配合:以 CMake 为例 4.加速构建的关键技巧 5.Ninja 与…

开发避坑指南(35):mybaits if标签test条件判断等号=解析异常解决方案

异常信息 org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.BuilderException: The expression orderInfo.idList evaluated to a null value.报错语句 <if test"orderInfo.queryFlag ! null and orderInfo.queryFlag sett…

GitCode 疑难问题诊疗:全面指南与解决方案

引言 在软件开发的动态领域中&#xff0c;GitCode 作为一款强大的分布式版本控制系统&#xff0c;已然成为团队协作与项目管理的基石。它赋予开发者高效管理代码版本、轻松实现并行开发以及顺畅协同合作的能力。然而&#xff0c;如同任何复杂的技术工具&#xff0c;在 GitCode…

使用 JS 渲染页面并导出为PDF 常见问题与修复

本文直击两个最常见的导出痛点&#xff0c;并给出可直接落地的诊断 修复方案&#xff08;适用于 html2canvas jsPDF ECharts/自绘 canvas 场景&#xff09;。 问题清单 问题 A&#xff1a;导出后图表模糊&#xff0c;线条与文字不清晰&#xff08;低分辨率&#xff09;。问题…

【Java后端】【可直接落地的 Redis 分布式锁实现】

可直接落地的 Redis 分布式锁实现&#xff1a;包含最小可用版、生产可用版&#xff08;带 Lua 原子解锁、续期“看门狗”、自旋等待、可重入&#xff09;、以及基于注解AOP 的无侵入用法&#xff0c;最后还给出 Redisson 方案对比与踩坑清单。一、设计目标与约束 获取锁&#x…

数据结构 -- 链表--双向链表的特点、操作函数

双向链表的操作函数DouLink.c#include "DouLink.h" #include <stdio.h> #include <stdlib.h> #include <string.h>/*** brief 创建一个空的双向链表* * 动态分配双向链表管理结构的内存&#xff0c;并初始化头指针和节点计数* * return 成功返回指…

Wireshark获取数据传输的码元速率

一、Wireshark的物理层参数 Wireshark主界面可以看到数据发送时刻和长度&#xff1a; 这个时刻是Wireshark完整获取数据包的时刻&#xff0c;实际上就是结束时刻。 需要知道的是&#xff1a; Wireshark工作在数据链路层及以上&#xff0c;它能解码 以太网帧 / IP 包 / TCP…

11.1.3 完善注册登录,实现文件上传和展示

1、完善注册/登录 1. 涉及的数据库表单&#xff1a;user_info 2. 引用MySQL线程池&#xff0c;Redis线程池 3. 完善注册功能 4. 完善登录功能 2.1 涉及的数据库表单&#xff1a;user_info 重新创建数据库 #创建数据库 DROP DATABASE IF EXISTS 0voice_tuchuang;CREATE D…

【Linux文件系统】目录结构

有没有刚进入Linux世界时&#xff0c;对着黑乎乎的终端&#xff0c;输入一个 ls / 后&#xff0c;看着蹦出来的一堆名字 like bin, etc, usr&#xff0c;感觉一头雾水&#xff0c;像是在看天书&#xff1f; 别担心&#xff0c;你不是一个人。Linux的文件系统就像一个超级有条理…

螺旋槽曲面方程的数学建模与偏导数求解

螺旋槽曲面的数学描述 在钻头设计和机械加工领域,螺旋槽的几何建模至关重要。螺旋槽通常由径向截形绕轴做螺旋运动形成,其数学模型可通过参数方程和隐函数方程两种方式描述。 设螺旋槽的径向截形方程为: y=f(z)y = f(z)y=f(z) x=xcx = x_cx=xc​ 其中 xcx_cxc​ 为常数,…

线性回归:机器学习中的基石

在机器学习的众多算法中&#xff0c;线性回归无疑是最基础也是最常被提及的一种。它不仅在统计学中占有重要地位&#xff0c;而且在预测分析和数据建模中也发挥着关键作用。本文将深入探讨线性回归的基本概念、评估指标以及在实际问题中的应用&#xff0c;并通过一个模拟的气象…

编程刷题-资料分发1 图论/DFS

P2097 资料分发 1 题目描述 有一些电脑&#xff0c;一部分电脑有双向数据线连接。 如果一个电脑得到数据&#xff0c;它可以传送到的电脑都可以得到数据。 现在&#xff0c;你有这个数据&#xff0c;问你至少将其输入几台电脑&#xff0c;才能使所有电脑得到数据。 输入格式 第…

RabbitMQ:延时消息(死信交换机、延迟消息插件)

目录一、死信交换机【不推荐】二、延迟消息插件【推荐】2.1 安装插件【Linux】2.2 安装插件【Windows】2.3 如何使用延时消息&#xff1a;生产者发送消息时指定一个时间&#xff0c;消费者不会立刻收到消息&#xff0c;而是在指定时间之后才收到消息。 延时任务&#xff1a;设置…

动学学深度学习05-深度学习计算

动学学深度学习pytorch 参考地址&#xff1a;https://zh.d2l.ai/ 文章目录动学学深度学习pytorch1-第05章-深度学习计算1. 层&#xff08;Layer&#xff09;与块&#xff08;Block&#xff09;1.1 什么是深度学习中的“层”&#xff1f;1.2 什么是“块”&#xff08;Block&…

智慧工厂烟雾检测:全场景覆盖与精准防控

智慧工厂烟雾检测&#xff1a;构建工业安全的智能防线&#xff08;所有图片均为真实项目案例&#xff09;在工业4.0时代&#xff0c;智慧工厂通过物联网、人工智能与大数据技术的深度融合&#xff0c;实现了生产流程的数字化与智能化。然而&#xff0c;工厂环境中的火灾隐患始终…

@JsonIgnoreProperties注解详解

JsonIgnoreProperties是 Jackson 库中的一个重要注解&#xff0c;用于在 JSON 序列化&#xff08;对象转 JSON&#xff09;和反序列化&#xff08;JSON 转对象&#xff09;过程中​​控制属性的可见性​​。它提供了更高级别的属性忽略能力&#xff0c;特别适合处理复杂场景。一…