手写koa2

44 篇文章 0 订阅

​一、koa2核心设计

  1. 封装node http server,创造Koa类构造函数

  2. 构造request、response、及context对象

  3. 中间件机制实现

二、koa2核心代码实现

    1.主文件koa.js

const http = require('http');const context = require('./context');const request = require('./request');const response = require('./response');class Koa {  constructor() {    this.middlewares = [];  }  listen(...args) {    http.createServer(async (req, res) => {      // 创建上下文对象      const ctx = this.createContext(req, res);      // 将middlewares合并成一个      const fn = this.compose(this.middlewares);      await fn(ctx);      // 给用户返回数据      res.end(ctx.body);    }).listen(...args);  }  use(mid) {    this.middlewares.push(mid);  }  createContext(req, res) {    const ctx = Object.create(context);    ctx.request = Object.create(request);    ctx.response = Object.create(response);    ctx.req = ctx.request.req = req;    ctx.res = ctx.response.res = res;    return ctx;    }  compose(middlewares) { // 中间件机制实现    return function(ctx) {      return dispatch(0); // 执行第0个      function dispatch(i) {        let fn = middlewares[i];        if (!fn) {          return Promise.resolve();        }        return Promise.resolve(          fn(ctx, function next() {            // promise完成后,再执行下一个            return dispatch(i + 1);                    });        )         }    }  }}

    2、封装request:request.js

module.exports = {  get url() {    return this.req.url;  }}

    3、封装response:response.js

module.exports = {  get body() {    return this._body;  }  set body(val) {    this._body = val;    }}

    4、封装context:context.js

module.exports = {  get url() {    return this.request.url;  }  get body() {    return this.response.body;  }  set body(val) {    this.response.body = val;  }}

以上为koa2的核心实现,仅作为学习和理解核心原理用,实际中的koa2有更多详尽的实现,具体查阅https://github.com/koajs/koa/tree/master/lib。

参考资料

  1. https://study.miaov.com/study/show/chapter/639

延伸阅读


▶ Walkthrough007

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值