使用 KOA 调用 ES6 class 中间件

Calling an ES6 class middleware using KOA

我在 ES6 class 中有一个函数,我试图将其作为中间件调用,但没有成功。在我的索引文件中,我有:

*我只包含相关代码

"use strict";
    const http        = require('http')
    const Koa         = require('koa')
    const app         = new Koa()
    const bodyParser  = require('koa-better-body')
    const Install     = require('./.system/install/install')   
    const install     = new Install()

class index {
  /**
  * Class constructor function
  * @Class Index
  * @method constructor
  * @return {undefined}
  */
  constructor () {
  }

  /**
  * Main entry method 
  * @Class Index
  * @method init async
  * @return {undefined}
  */
  async init(){
    // install helper
    app.use(install.setup)

    // response
    app.use(async ctx => {
      ctx.body = 'Hello World';
    });

    http.createServer(app.callback()).listen(8080);
  }
}

let init = new index(); init.init()

在我的 install.js 中,我有:

       "use strict";
        const process = require('./library/process')
        const fs      = require('async-file')

    module.exports = class install {
        async setup (ctx, next) { 
            ctx.body = 'install';
        }
    }

但是 koa 应用程序一直给我一个 404 未找到错误。我知道静态方法可能会解决这个问题,但我不想使用静态方法。安装class既有中间件功能又有标准功能。

你应该在你的中间件函数中调用 next()

你有404,因为你没有使用koa-router