当前位置:网站首页>koa框架(一)
koa框架(一)
2022-08-10 22:38:00 【Silent Land】
koa简介
功能
- 提供基础的HTTP应用服务
核心概念
- 应用(Koa Application)
- 上下文 (Context)
- 请求(Request)
- 响应(Response)

特点
- 轻量/简洁
- async/await
- 丰富的中间件
koa使用
安装
Koa需要 node v7.6.0或更高版本来支持ES2015、异步方法
npm install -S koa
常用api
app.use
- 为应用添加指定的中间件
app.listen
如下为一个绑定3000端口的简单 Koa 应用,其创建并返回了一个 HTTP 服务器,为 Server#listen() 传递指定参数(参数的详细文档请查看nodejs.org)。
const Koa = require('koa')
const app = new Koa()
app.listen(3000)
- app.on
- 事件侦听
koa中间件
- koa的工作原理
- 执行的顺序:顺序执行
- 回调的顺序:反向执行
- 先进后出

const Koa = require('koa')
const app = new Koa()
const middleware = function async (ctx, next) {
console.log('this is a widdleware')
console.log(ctx.request.path)
next()
}
const middleware1 = function async (ctx, next) {
console.log('this is a widdleware1')
console.log(ctx.request.path)
next()
console.log(1111111)
}
const middleware2 = function async (ctx, next) {
console.log('this is a widdleware2')
console.log(ctx.request.path)
next()
console.log(22222)
}
const middleware3 = function async (ctx, next) {
console.log('this is a widdleware3')
console.log(ctx.request.path)
next()
console.log(3333)
}
app.use(middleware)
app.use(middleware1)
app.use(middleware2)
app.use(middleware3)
app.listen(3000)
打印
this is a widdleware
/
this is a widdleware1
/
this is a widdleware2
/
this is a widdleware3
/
3333
22222
1111111
this is a widdleware
/favicon.ico
this is a widdleware1
/favicon.ico
this is a widdleware2
/favicon.ico
this is a widdleware3
/favicon.ico
3333
22222
1111111
Koa 的中间件通过一种更加传统(您也许会很熟悉)的方式进行级联,摒弃了以往 node 频繁的回调函数造成的复杂代码逻辑。 然而,使用异步函数,我们可以实现"真正" 的中间件。与之不同,当执行到 yield next 语句时,Koa 暂停了该中间件,继续执行下一个符合请求的中间件(‘downstrem’),然后控制权再逐级返回给上层中间件(‘upstream’)。
常用插件
路由
koa-router
- 安装
npm install -S koa-router
- 使用
const Koa = require('koa')
const Router = require('koa-router')
const app = new Koa()
const router = new Router()
router.get('/', ctx => {
console.log('ctx', ctx)
ctx.body = 'Hello World'
})
// 路由定义的方法定义到koa应用中
app.use(router.routes()).use(router.allowedMethods())
app.listen(3000)
跨域处理
@koa/cors
- 安装
npm install -S @koa/cors
- 使用
const cors = require('@koa/cors')
app.use(cors())
压缩
koa-compress
静态资源
koa-static
- 安装
npm install -S koa-static
- 使用
const path = require('path')
const staticServer = require('koa-static')
app.use(staticServer(path.join(__dirname, 'static')))
协议处理
- koa-json
- 安装
npm install koa-json -S
- 使用
const json = require('koa-json')
// 请求上拼接pretty时格式化
app.use(json({
pretty: false, param: 'pretty'}))
- koa-body
- 安装
npm install koa-body -S
- 使用
const koabody = require('koa-body')
app.use(koabody())
安全
- 鉴权方式
- koa-session
- koa-jwt
- 通信头
- koa-helmet
日志
koa-logger
边栏推荐
- 自学软件测试不知道该如何学起,【软件测试技能图谱|自学测试路线图】
- RK3399 platform development series explanation (kernel-driven peripherals) 6.35, IAM20680 gyroscope introduction
- 鹏城杯 2022 web/misc writeup
- 分享一个后台管理系统可拖拽式组件的设计思路
- 配电网络扩展规划:考虑使用概率性能源生产和消费概况的决策(Matlab代码实现)
- "DevOps Night Talk" - Pilot - Introduction to CNCF Open Source DevOps Project DevStream - feat. PMC member Hu Tao
- 德科立科创板上市:年营收7.3亿 市值59亿
- 链表相加(二)
- Pro-test is effective | A method to deal with missing features of risk control data
- fme csmapreprojector转换器使用高程异常模型进行高程基准转换
猜你喜欢

高数_复习_第5章:多元函数微分学

阿里云新增三大高性能计算解决方案,助力生命科学行业快速发展

高通平台开发系列讲解(应用篇)QCMAP应用框架介绍

确诊了!是Druid1.1.20的锅,查询无法映射LocalDateTime类型(带源码解析及解决方案)

August 10, 2022: Building Web Applications for Beginners with ASP.NET Core -- Creating Web UIs with ASP.NET Core

This visual tool artifact is more intuitive and easy to use!love so much

“数据引擎”开启前装规模量产新赛道,「智协慧同」崭露头角

Pro-test is effective | A method to deal with missing features of risk control data

CFdiv2-Beautiful Mirrors-(期望)

链表相加(二)
随机推荐
二叉树 | 层序遍历 | leecode刷题笔记
The Missing Semester of Your CS Education
ArcGIS中的坐标系统和投影变换
腾讯云轻量应用服务器配置及建网站教程
Introduction to the use of counter instructions in Rockwell AB PLC RSLogix5000
【MySQL】mysql因为字符集导致left join出现Using join buffer (Block Nested Loop)
虚拟地址空间
leetcode:357. 统计各位数字都不同的数字个数
file IO-buffer
2021 IDEA creates web projects
BM13 determines whether a linked list is a palindrome
MySQL performance schema性能分析实战
KRONES克朗斯电源维修0-901-17-350-8技术概论
2021IDEA创建web工程
Pro-test is effective | A method to deal with missing features of risk control data
JS中使用正则表达式g模式和非g模式的区别
XSLeaks 侧信道攻击 (unfinished)
阿里云架构师金云龙:基于云XR平台的视觉计算应用部署
The Thread State,
【uniapp】uniapp微信小程序开发:启动微信开发者工具提示no such file or directory错误