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

推荐订阅源

aimingoo的专栏
aimingoo的专栏
G
Google Developers Blog
B
Blog RSS Feed
A
About on SuperTechFans
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V
V2EX
Stack Overflow Blog
Stack Overflow Blog
C
Check Point Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Engineering at Meta
Engineering at Meta
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 司徒正美
D
Docker
F
Fortinet All Blogs
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
H
Help Net Security
WordPress大学
WordPress大学
MyScale Blog
MyScale Blog
博客园 - Franky
人人都是产品经理
人人都是产品经理
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Blog — PlanetScale
Blog — PlanetScale
L
LangChain Blog

6Jyc5p+a

【笔记】HermesAgent学习笔记 【笔记】DeepSeekHarness学习笔记 【笔记】Rollup的PluginBabel插件 【笔记】Rollup学习笔记 【笔记】Gulp的BrowserSync插件 【笔记】Node-SSH学习笔记 【笔记】Tapable学习笔记 【笔记】Highlight.js学习笔记 【笔记】Marked学习笔记 【笔记】Webpack通过schema-utils实现Loader参数校验 【笔记】Webpack实现TreeShaking 【笔记】Webpack的WebpackBundleAnalyzer插件 【代码】校验信用卡号码 【笔记】Webpack的SpeedMesureWebpackPlugin插件 【笔记】Codex配置Brave浏览器的MCP 【笔记】Webpack的CSSMinimizerWebpackPlugin插件 【笔记】FastNodeManager学习笔记 【笔记】Terser学习笔记 【笔记】Codex配置Wireshark的MCP 【笔记】Codex配置Unity的MCP 【笔记】Codex配置Blender的MCP 【笔记】Codex配置IDEA的MCP 【笔记】Browserslist学习笔记 【笔记】Codex学习笔记 【笔记】MicrosoftStore微软商店学习笔记 【笔记】通过NapCat和AstrBot实现QQ机器人 【笔记】部署NapCat 【笔记】部署AstrBot 【笔记】Poetry学习笔记 【笔记】uv学习笔记
【笔记】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;
});

完成