实现自己的Koa2

Koa就是基于node自带的http模块,经过封装,监听端口,实现ctx(上下文)管理,中间件管理等

例子1、koa监听3000端口,在页面输出值

 1 const Koa = require('koa')
 2 const app = new Koa()
 3 
 4 app.use((ctx) => {
 5   ctx.body = 'hello koa2'
 6 })
 7 
 8 app.listen(3000, function () {
 9   console.log('启动3000端口')
10 })

例子2、http监听3000端口,页面返回值

1 const http = require('http')
2 
3 const server = http.createServer((req,res) => {
4   res.writeHead('200')
5   res.end('hello node')
6 })
7 server.listen(3000, function () {
8   console.log('启动了3000端口')
9 })

例子3、使用http封装一个简单的web服务

 1 const http = require('http')
 2 
 3 class application{
 4   constructor() {
 5     this.callback = () => {}
 6   }
 7 
 8   use(callback) {
 9     this.callback = callback
10   }
11 
12   listen(...args) {
13     const server = http.createServer((req,res) => {
14       this.callback(req, res)
15     })
16     server.listen(...args)
17   }
18 }
19 
20 module.exports = application
 1 const Koa3 = require('./index3')
 2 const app = new Koa3()
 3 
 4 app.use((req,res) => {
 5   res.writeHead(200)
 6   res.end('hello Koa3')
 7 })
 8 
 9 app.listen(3003, function () {
10   console.log('启动3003端口')
11 })

例子4:

koa2中的ctx就是上下文,用来挂载request和response对象

js的get和set方法

 1 const yese = {
 2   _name: '夜色',
 3   get name() {
 4     return this._name
 5   },
 6   set name(val) {
 7     console.log('new name is' + val)
 8     this._name = val
 9   }
10 }
11 
12 console.log(yese.name)
13 yese.name = '荷塘月色'
14 console.log(yese.name)

加入ctx上下文,封装了http里的request和response

 1 const http = require('http')
 2 
 3 //req是http模块里的
 4 let request = {
 5   get url () {
 6     return this.req.url
 7   }
 8 }
 9 
10 let response = {
11   get body () {
12     return this._body
13   },
14   set body (val) {
15     this._body = val
16   }
17 }
18 
19 // 把上面定义的request和response挂载到context对象中
20 let context = {
21    get url () {
22      return this.request.url
23    },
24    get body () {
25      return this.response.body
26    },
27   set body (val) {
28      this.response.body = val
29   }
30 }
31 
32 
33 class application{
34   constructor() {
35     // 把上面定义的context,request,response挂载到application中
36     this.context = context
37     this.request = request
38     this.response = response
39   }
40 
41   use(callback) {
42     this.callback = callback
43   }
44   listen(...args) {
45     const server = http.createServer(async (req, res) => {
46       let ctx = this.createCtx(req,res)
47       await this.callback(ctx)
48       ctx.res.end(ctx.body)
49     })
50     server.listen(...args)
51   }
52   createCtx (req, res) {
53     let ctx = Object.create(this.context)
54     ctx.request = Object.create(this.request)
55     ctx.response = Object.create(this.response)
56     // 把http里的req赋值给ctx.request的req和ctx.req上
57     ctx.req = ctx.request.req = req
58     ctx.res = ctx.response.req = res
59     return ctx
60   }
61 }
62 
63 module.exports = application

调用

 1 const Koa3 = require('./index7')
 2 const app = new Koa3()
 3 
 4 app.use(async (ctx) => {
 5   ctx.body = 'hello Koa2 '+ ctx.url
 6 })
 7 
 8 app.listen(3003, function () {
 9   console.log('启动3003端口')
10 })

猜你喜欢

转载自www.cnblogs.com/zhaobao1830/p/9333524.html