Skip to content

Commit 3c69b0e

Browse files
author
142vip.cn
committed
feat(koa): 大幅新增Koa框架教程、文档
1 parent b039c36 commit 3c69b0e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1785
-766
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<div align="center">
44
<img alt="JavaScriptCollection" src="https://cdn.statically.io/gh/142vip/cdn_service@main/doc_book/jsc/jsc_logo.png" style="text-align: center;">
5-
<p style="font-size: 30px"><strong>✨【理论+实战】保姆级成长笔记✨</strong></p>
5+
<p style="font-size: 20px"><strong>✨【理论+实战】保姆级成长笔记✨</strong></p>
66
</div>
77

88
<div align="center">

code/koa/http-status.txt

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
- 100 "continue"
2+
- 101 "switching protocols"
3+
- 102 "processing"
4+
- 200 "ok"
5+
- 201 "created"
6+
- 202 "accepted"
7+
- 203 "non-authoritative information"
8+
- 204 "no content"
9+
- 205 "reset content"
10+
- 206 "partial content"
11+
- 207 "multi-status"
12+
- 208 "already reported"
13+
- 226 "im used"
14+
- 300 "multiple choices"
15+
- 301 "moved permanently"
16+
- 302 "found"
17+
- 303 "see other"
18+
- 304 "not modified"
19+
- 305 "use proxy"
20+
- 307 "temporary redirect"
21+
- 308 "permanent redirect"
22+
- 400 "bad request"
23+
- 401 "unauthorized"
24+
- 402 "payment required"
25+
- 403 "forbidden"
26+
- 404 "not found"
27+
- 405 "method not allowed"
28+
- 406 "not acceptable"
29+
- 407 "proxy authentication required"
30+
- 408 "request timeout"
31+
- 409 "conflict"
32+
- 410 "gone"
33+
- 411 "length required"
34+
- 412 "precondition failed"
35+
- 413 "payload too large"
36+
- 414 "uri too long"
37+
- 415 "unsupported media type"
38+
- 416 "range not satisfiable"
39+
- 417 "expectation failed"
40+
- 418 "I'm a teapot"
41+
- 422 "unprocessable entity"
42+
- 423 "locked"
43+
- 424 "failed dependency"
44+
- 426 "upgrade required"
45+
- 428 "precondition required"
46+
- 429 "too many requests"
47+
- 431 "request header fields too large"
48+
- 500 "internal server error"
49+
- 501 "not implemented"
50+
- 502 "bad gateway"
51+
- 503 "service unavailable"
52+
- 504 "gateway timeout"
53+
- 505 "http version not supported"
54+
- 506 "variant also negotiates"
55+
- 507 "insufficient storage"
56+
- 508 "loop detected"
57+
- 510 "not extended"
58+
- 511 "network authentication required"

code/koa/koa-app.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const Koa = require('koa')
2+
3+
// 利用构造函数修改属性
4+
const app1 = new Koa({ proxy: true })
5+
console.log(app1)
6+
7+
8+
// 定义创建完动态修改属性
9+
const app2 = new Koa()
10+
app2.proxy = true
11+
console.log(app2)

code/koa/koa-context.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const Koa = require('koa')
2+
const app = new Koa()
3+
app.use(async(ctx, next) => {
4+
// Context对象
5+
console.log(ctx)
6+
// Koa的Request对象
7+
console.log(ctx.request)
8+
// Koa的Response对象
9+
console.log(ctx.response)
10+
11+
await next()
12+
})
13+
14+
app.use(ctx => {
15+
// ctx.app是对app的引用
16+
console.log(ctx.app === app)
17+
ctx.cookies.set()
18+
ctx.throw(400)
19+
ctx.throw(400, '参数错误')
20+
ctx.throw(400, '参数错误', { message: '缺少必要参数' })
21+
22+
ctx.assert()
23+
})
24+
25+
26+
app.use(async(ctx, next) => {
27+
ctx.body = 'hello world'
28+
ctx.status = 200
29+
ctx.res.statusCode = 500
30+
})
31+
32+
33+
app.listen(8000)
34+
console.log('listening on port 8000')

code/koa/koa-listen.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const Koa = require('koa')
2+
const app = new Koa()
3+
// 监听服务端端口3000
4+
app.listen(3000)
5+
6+
7+
// 语法糖实现
8+
const http = require('http')
9+
const server = http.createServer(app.callback())
10+
// 监听端口
11+
server.listen(3000)
12+
13+
const https = require('https')
14+
const httpsServer = https.createServer(app.callback())
15+
httpsServer.listen(6000)

code/koa/koa-request.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const Koa = require('koa')
2+
const app = new Koa()
3+
4+
app.use(async(ctx, next) => {
5+
ctx.request.header = {
6+
reference: 'https://code.142vip.cn'
7+
}
8+
await next()
9+
})
10+
11+
app.listen(6000)
12+
console.log('listening on port 6000')

code/koa/koa-response.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const minify = require('html-minifier')
2+
const Koa = require('koa')
3+
const app = new Koa()
4+
app.use(async(ctx, next) => {
5+
await next()
6+
7+
if (!ctx.response.is('html')) return
8+
9+
let body = ctx.body
10+
if (!body || body.pipe) return
11+
12+
if (Buffer.isBuffer(body)) body = body.toString()
13+
ctx.body = minify(body)
14+
})
15+
16+
app.listen(7000)
17+
console.log('listening on port 7000')

code/koa/koa-start.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const Koa = require('koa')
2+
const app = new Koa()
3+
4+
app.use(async ctx => {
5+
ctx.body = 'Hello World'
6+
})
7+
8+
app.listen(3000)

0 commit comments

Comments
 (0)