Socket
Socket
Sign inDemoInstall

mkbugjs

Package Overview
Dependencies
69
Maintainers
1
Versions
44
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    mkbugjs

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


Version published
Maintainers
1
Install size
20.1 kB
Created

Readme

Source

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.jsEgg.jsThink.js
NodejsNodejs 10+Nodejs 8+Nodejs 6+
Base onExpress.jsKoa.jsKoa.js
RouterAutoManualAuto
PluginAutoManualManual
MiddlewareAuto+ManualManualManual
ConfigAutoNoNo
JS extendES6ES6Babel
StyleOOPPurePure
DurationYesNoNo
Extend Capabilitycompatible expressjsegg ecologycompatible 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?

Keywords

FAQs

Last updated on 31 Dec 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc