繁体   English   中英

使用KOA调用ES6类中间件

[英]Calling an ES6 class middleware using KOA

我试图将ES6类中的一个函数称为中间件,但没有成功。 在我的索引文件中,我有:

*我只包含相关代码

"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 not found错误。 我知道静态方法可能会解决此问题,但我宁愿不使用静态方法。 安装类同时具有中间件功能和标准功能。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM