惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Security Latest
Security Latest
S
Schneier on Security
The Hacker News
The Hacker News
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
T
Troy Hunt's Blog
云风的 BLOG
云风的 BLOG
Scott Helme
Scott Helme
I
Intezer
PCI Perspectives
PCI Perspectives
博客园_首页
量子位
E
Exploit-DB.com RSS Feed
Martin Fowler
Martin Fowler
aimingoo的专栏
aimingoo的专栏
Cloudbric
Cloudbric
V
V2EX - 技术
M
MIT News - Artificial intelligence
Latest news
Latest news
T
The Exploit Database - CXSecurity.com
The Register - Security
The Register - Security
S
SegmentFault 最新的问题
Simon Willison's Weblog
Simon Willison's Weblog
Help Net Security
Help Net Security
有赞技术团队
有赞技术团队
T
Tor Project blog
P
Proofpoint News Feed
美团技术团队
L
LINUX DO - 最新话题
Vercel News
Vercel News
P
Privacy International News Feed
A
About on SuperTechFans
U
Unit 42
罗磊的独立博客
S
Security Affairs
T
Tailwind CSS Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
阮一峰的网络日志
阮一峰的网络日志
Recent Commits to openclaw:main
Recent Commits to openclaw:main
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Stack Overflow Blog
Stack Overflow Blog
D
Docker
Hugging Face - Blog
Hugging Face - Blog
L
Lohrmann on Cybersecurity
S
Security Archives - TechRepublic
N
News | PayPal Newsroom
S
Security @ Cisco Blogs
博客园 - 三生石上(FineUI控件)
Attack and Defense Labs
Attack and Defense Labs
Recent Announcements
Recent Announcements
P
Proofpoint News Feed

6Jyc5p+a

【代码】Python3实现从NTP服务器同步时间 【笔记】Solidity计算字符串的MD5值 【笔记】Hardhat学习笔记 【笔记】Go安装笔记 【笔记】CVE-2024-3094漏洞利用 【笔记】CVE-2026-43284和CVE-2026-43500漏洞利用 【笔记】CVE-2023-3567漏洞利用 【代码】Python3读写M1卡 【笔记】M1卡学习笔记 【笔记】Python3中文转拼音 【代码】Python3生成中国大陆姓名拼音 【代码】Python3爬取中国大陆手机号段 【笔记】Nodejs发送请求 【笔记】Trello通过API添加待办事项 【笔记】Nodejs的流和缓冲区 【笔记】Nodejs的事件 【笔记】Nodejs的文件和目录操作 【笔记】CNVD-2020-10487漏洞利用 【笔记】CVE-2017-12617漏洞利用 【笔记】PHP输出源码 【笔记】PHP的Phar 【笔记】通过Docker部署OnlineTools 【笔记】XML学习笔记 【笔记】Windows的用户和组 【笔记】CVE-2006-7243漏洞利用 【代码】JS将目录编号转换为十六进制 【笔记】PHP抑制所有报错 【笔记】HFish学习笔记 【笔记】JumpServer学习笔记 【笔记】Conpot学习笔记 【笔记】南墙WAF学习笔记 【笔记】堡塔云WAF学习笔记 【笔记】Windows的远程桌面服务 【笔记】Windows的防火墙
【笔记】Koa学习笔记
6Jyc5p+a · 2026-06-16 · via 6Jyc5p+a

前言

Expressive HTTP middleware framework for node.js to make web applications and APIs more enjoyable to write.(Github

下载依赖

1
npm install koa

引入依赖

1
const Koa = require("koa");

创建Web服务器

1
2
3
4
5
const app = new Koa();
app.use(function (context, next) {
context.body = "";
});
app.listen(8080);
1
2
3
4
5
6
7
const app = new Koa();
app.use(function (context, next) {
context.body = "";
});
app.listen(8080, function () {
console.log(`http://127.0.0.1:8080`);
});
1
2
3
4
5
6
7
const app = new Koa();
app.use(function (context, next) {
context.body = "";
});
app.listen(8080, "0.0.0.0", function () {
console.log(`http://127.0.0.1:8080`);
});

中间件

1
2
3
app.use(function (context, next) {
...
});

继续下一个中间件

1
next();

处理请求

获取Nodejs请求对象

1
const request = context.req;

获取Koa请求对象

1
const request = context.request;

获取请求行

获取请求URL

1
const path = context.request.path;
1
const path = context.path;

获取请求方法

1
const method = context.request.method;
1
const method = context.method;

处理响应

获取Nodejs响应对象

1
const response = context.res;

获取Koa响应对象

1
const response = context.response;

设置响应体并返回响应

1
context.body = "";

路由

下载依赖

1
npm install @koa/router

引入依赖

1
const KoaRouter = require("@koa/router");

定义路由

  • 如果没有注册router.allowedMethods(),则访问没有匹配的路由时会返回Not Found
  • 如果注册了router.allowedMethods(),则访问没有匹配的路由时会返回Method Not Allowed
1
2
3
4
5
6
7
8
const router = new KoaRouter({ prefix: "/api" });

router.get("/", function (context, next) {
...
});

app.use(router.routes());
app.use(router.allowedMethods());

处理请求

获取请求参数

query
1
2
3
router.get("/", function (context, next) {
const query = context.query;
});
path
1
2
3
router.get("/:id", function (context, next) {
const id = context.params.id;
});

解析Form请求体和JSON请求体

下载依赖

1
npm install koa-bodyparser

解析Form请求参数和JSON请求参数

1
2
3
4
5
6
7
const bodyParser = require("koa-bodyparser");

app.use(bodyParser());

app.use(function (context, next) {
const body = context.request.body;
});

解析FormData请求参数

下载依赖

1
npm install @koa/multer multer

解析FormData请求参数

1
2
3
4
5
6
7
const multer = require("@koa/multer");

const formParser = multer();

app.use(formParser.any(), function (context, next) {
const body = context.request.body;
});

解析单个文件上传请求参数

1
2
3
4
5
6
7
const multer = require("@koa/multer");

const upload = multer({ dest: "./uploads" });

app.use(upload.single("key"), function (context, next) {
const file = context.request.file;
});
  • 自定义文件名
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const multer = require("@koa/multer");

const upload = multer({
storage: multer.diskStorage({
destination: function (request, file, callback) {
callback(null, "./uploads");
},
filename: function (request, file, callback) {
callback(null, file.originalname);
}
})
});

app.use(upload.single("key"), function (context, next) {
const file = context.request.file;
});

解析多个文件上传请求参数

1
2
3
4
5
6
7
const multer = require("@koa/multer");

const upload = multer({ dest: "./uploads" });

app.post("/", upload.array("key"), function (context, next) {
const files = context.request.files;
});

完成