狭义中间件

前言

狭义中间件的要素常见要素如下所示。

  • 一切皆中间件
  • 中间件内操作请求 request
    • 请求拦截
  • 中间件内操作响应 response
    • 响应拦截
  • 中间件内操作上下文 context
    • 直接上下文代理,初始化实例时候挂载代理在app.context
    • 请求过程上下文代理,请求时候挂载代理在ctx
  • 大部分直接被 app.use() 加载
    • 注意: 初始化实例挂载代理context不被app.use()

请求拦截

const Koa = require('koa');
let app = new Koa();

const middleware = async function(ctx, next) {
  // 中间件 拦截请求
  // 把所有请求不是 /page/ 开头的路径全部抛出500错误
  const reqPath = ctx.request.path;
  if( reqPath.indexOf('/page/') !== 0 ) {
    ctx.throw(500)
  }
  await next();
}

const page = async function(ctx, next) {
  ctx.body = `
      <html>
        <head></head>
        <body>
          <h1>${ctx.request.path}</h1>
        </body>
      </html>
    `; 
}

app.use(middleware);
app.use(page);

app.listen(3001, function(){
  console.log('the demo is start at port 3001');
})

响应拦截

const Koa = require('koa');
let app = new Koa();

const middleware = async function(ctx, next) {
  ctx.response.type = 'text/plain';
  await next();
}

const page = async function(ctx, next) {
  ctx.body = `
      <html>
        <head></head>
        <body>
          <h1>${ctx.path}</h1>
        </body>
      </html>
    `; 
}

app.use(middleware);
app.use(page);

app.listen(3001, function(){
  console.log('the demo is start at port 3001');
})

context挂载代理

  • 请求代理注入

    • 直接被app.use
    • 请求时候才有注入
    • 每次请求的注入都不同
  • 初始化实例(应用)代理注入

    • 直接注入到app.context
    • 初始化应用的时候才注入
    • 只注入一次,每次请求都可以使用

请求时挂载代理context

const Koa = require('koa');
let app = new Koa();

const middleware = async function(ctx, next) {
  // 中间件 代理/挂载上下文
  // 把所有当前服务的进程PID,内存使用情况方法代理/挂载在ctx上
  ctx.getServerInfo = function() {
    function parseMem( mem = 0 ) {
      let memVal = mem / 1024 / 1024;
      memVal = memVal.toFixed(2) + 'MB';
      return memVal;
    }

    function getMemInfo() {
      let memUsage = process.memoryUsage();
      let rss = parseMem(memUsage.rss);
      let heapTotal = parseMem(memUsage.heapTotal);
      let heapUsed =  parseMem(memUsage.heapUsed);
      return {
        pid: process.pid,
        rss,
        heapTotal,
        heapUsed
      }
    }
    return getMemInfo()
  };
  await next();
}

const page = async function(ctx, next) {
  const serverInfo = ctx.getServerInfo();
  ctx.body = `
      <html>
        <head></head>
        <body>
          <p>${JSON.stringify(serverInfo)}</p>
        </body>
      </html>
    `; 
}

app.use(middleware);
app.use(page);

app.listen(3001, function(){
  console.log('the demo is start at port 3001');
})

初始化实例挂载代理context

const Koa = require('koa');
let app = new Koa();

const middleware = function(app) {
  // 中间件在初始化实例 把getServerInfo方法 挂载代理到上下文
  app.context.getServerInfo = function() {
    function parseMem( mem = 0 ) {
      let memVal = mem / 1024 / 1024;
      memVal = memVal.toFixed(2) + 'MB';
      return memVal;
    }

    function getMemInfo() {
      let memUsage = process.memoryUsage();
      let rss = parseMem(memUsage.rss);
      let heapTotal = parseMem(memUsage.heapTotal);
      let heapUsed =  parseMem(memUsage.heapUsed);
      return {
        pid: process.pid,
        rss,
        heapTotal,
        heapUsed
      }
    }
    return getMemInfo()
  };
}

middleware(app);

const page = async function(ctx, next) {
  const serverInfo = ctx.getServerInfo();
  ctx.body = `
      <html>
        <head></head>
        <body>
          <p>${JSON.stringify(serverInfo)}</p>
        </body>
      </html>
    `; 
}

app.use(page);

app.listen(3001, function(){
  console.log('the demo is start at port 3001');
})

results matching ""

    No results matching ""