Web Framework

General

  • 基础能力实现:
    • 路由
    • Cookie / Session
    • 数据请求
    • 静态资源
    • 模板引擎
    • 文件上传
    • 数据库交互
  • 实例动态可扩展问题:callback() 单一函数封装,方便多 app 实例的创建

Koa

Expressive HTTP middleware framework for node.js to make web applications and APIs more enjoyable to write. Koa’s middleware stack flows in a stack-like manner, allowing you to perform actions downstream then filter and manipulate the response upstream.

:::info 💎 The power of simplicity.
从文档的简洁程度就可以看出 Koa 设计的良苦用心:https://github.com/koajs/koa/blob/HEAD/docs/guide.md :::

Core

  • async / await
  • 洋葱圈模型
    • Koa.js的一个中间件引擎 koa-compose模块来实现的,也就是Koa.js实现洋葱模型的核心引擎
      • context injection as a whole
      • next() 做 FirstIn / LastOut 的控制流
      • 提前结束机制
  • Context / Response / Request 三个核心职能类的封装

设计思想

  • AOP:面向切面编程,典型

Express / Koa / Egg / Midway - 图1

Guide

  • recommend to read the examples of KOA implementations on specific situations.

Async middleware:

  1. function log( ctx ) {
  2. console.log( ctx.method, ctx.header.host + ctx.url )
  3. }
  4. module.exports = function () {
  5. return async function ( ctx, next ) {
  6. log(ctx);
  7. await next()
  8. }
  9. }
  • koa-compose 这个库比较有意思,可以将多个中间件函数合并为一个函数。其源代码实现可以参考 这里

再配合经典的 Koa 的洋葱 🧅 中间件调用手搓简单版本:

  1. export function composeMiddleware(...fnList: Function[]) {
  2. const execMiddlewareFn = async (fn: any) => {
  3. if (!fn) return;
  4. await fn.call(null, async () => {
  5. return new Promise(async (resolve) => {
  6. await execMiddlewareFn(fnList.shift());
  7. resolve();
  8. });
  9. });
  10. };
  11. return () => execMiddlewareFn(fnList?.shift());
  12. }

Egg

关键:解决企业级的 node.js 应用研发

问题:

  • 多人,多项目共同研发
  • One ring, cannot rule them all, different team has different scenarios and require different tech-stack

Design principles

  • 中间件可替换、可插拔:Middleware
  • 约定大于配置(Convention over configuration):Loader
  • MVC, model as service

Conventions

  • router.js
  • controller/
  • service/
  • middleware/(koa middleware)
  • public/
  • view/
  • extend/
  • *vendor/

理解 Service:

Service is an abstract layer which is used to encapsulate business logics in complex business circumstances, and this abstraction offers advantages as below:

  • keep logics in Controller cleaner.
  • keep business logics independent, since the abstracted Service can be called by many Controllers repeatedly.
  • separate logics and representations, and make it easier to write test cases.

Usage scenarios:

  • Processing complex data, e.g. information to be shown need to be got from databases, and should be processed in specific rules before it can be sent and seen by the user. Or when the process is done, the database should be updated.
  • Calling third party services, e.g. getting Github information etc.

Inset modules

  • plugin.js
  • config.*env.js
  • Router: use directory as routes
  • ViewEngine
  • Schedule
  • i18n

Good parts

Egg.js 框架专门为企业级应用设计了一套进程管理机制,兼顾了资源利用率,性能,连接数,负载均衡,健壮性以及低复杂度等方面。

  • TODO : HOW?

B-Egg / Midway

Koa.js + Egg.js + Aone + @ali/middlewares

Higher level topics

  • Multi-process model / IPC
  • Caching

Ref


经典理解