Skip to content

Latest commit

 

History

History
131 lines (107 loc) · 3.69 KB

README.md

File metadata and controls

131 lines (107 loc) · 3.69 KB

NPM version NPM quality npm download build status

mkbug.js

An OOP style declare Nodejs framework base on Express.js!

官方文档(中文)

What is mkbug.js

An OOP Style Restful Api framewrok base on Express.js,and make Nodejs development beautiful and easy.

Mkbug.js VS Egg.js VS Think.js

项目 Mkbug.js Egg.js Think.js
Nodejs Nodejs 10+ Nodejs 8+ Nodejs 6+
Base on Express.js Koa.js Koa.js
Router Auto Manual Auto
Plugin Auto Manual Manual
Middleware Auto+Manual Manual Manual
Config Auto No No
JS extend ES6 ES6 Babel
Style OOP Pure Pure
Duration Yes No No
Extend Capability compatible expressjs egg ecology compatible koa

Your First Mkbug Application

  // index.js
  const express = require('express');
  const app = express();

  const { Mkbug } = require('mkbugjs');

  new Mkbug(app)
    .create('/') // 请求url前缀
    .use(bodyParser.json()) // 使用express中间件
    .start(3001, (err) => { // 启动,同app.listen
    if (!err)
      console.log('Server started!')
    else
      console.error('Server start failed!')
  })

  // src/controller/index.js
  const { BaseController } = require('mkbugjs');

  module.exports = class api extends BaseController {
    getAction () {
      return 'Hello World';
    }
  }

About extends

If you want to use middleware like koa. you can use it like this.

  // base/ControllerBaseBase.js
  const { BaseController } = require('mkbug.js');

  module.exports = class ControllerBaseBase extends BaseController {
    before (req) {
      console.log("ControllerBaseBase before")
    }

    after(){
      console.log("ControllerBaseBase after")
    }
  }

  // base/ControllerBase.js
  const ControllerBaseBase = require('./ControllerBaseBase');

  module.exports = class ControllerBase extends ControllerBaseBase {
    before (request, response) {
      super.before(request, response)
      console.log("ControllerBase before")
    }

    after ({ duration, status, originalUrl, request, response }) {
      console.log("ControllerBase after")
      super.after({ duration, status, originalUrl, request, response })
    }
  }

  // ExtendsTest.js
  const ControllerBase = require('./base/ControllerBase');

  module.exports = class ExtendsTest extends ControllerBase {
    before (request, response) {
      super.before(request, response)
      console.log("Request start")
    }

    getAction () {
      return "hello world"
    }

    after () {
      console.log("Request end")
      super.after({})
    }
  }

And then you can send curl request to /api/extendstest, you should get the log:

  $ curl http://localhost:3000/api/extendstest

  ControllerBaseBase before
  ControllerBase before
  Request start
  Request end
  ControllerBase after
  ControllerBaseBase after

It is very easy, right?