1.0.2 • Published 7 years ago

matpgm v1.0.2

Weekly downloads
1
License
ISC
Repository
github
Last release
7 years ago

matpgm

Design

focus on better dev experience, but not any other things.

  • light, only for server, can easily embed with fe code.
  • helper you auto load midwares for whole server or special controllers.
  • helper you auto load controllers.
  • hot reload. hot reload dev-server for koa1 & koa2. reload the midwares, controller and helper.
  • no babel, use the native directly, got rid of babel issues.

Usage

install matpgm global: install the cli tool.

npm install -g matpgm

init a project: mkdir and auto install the init dependencis.

matpgm init project_name

run dev server

npm run sdev

build the server to production version.

npm run sbuild

Detail

the project tree:

|____bin
| |____boot.js
|____server
| |____controller
| | |____index
| | | |____index.js
| |____helper
| | |____logger.js
| |____midware
| | |_____.js
| | |____index.js

a controller file can provide more than one controller, this is a controller.

module.exports = [{
    path: '/user/:user/', //this path same as koa-router
    method: ['get'], // the methods
    controller: async ctx => {
        var user = ctx.params.user
        ctx.body = 'you are read ' + user + '\'s page';
    }
}, {
    path: '/user2/:user/',
    method: ['get'],
    controller: async ctx => {
        var user = ctx.params.user
        ctx.body = 'this is second page of ' + user;
    }
}]

midware are async function(koa2) or generator(koa1).

helpers are others.

we can indicate specific controllers use a group of specific midwares. this is why midware/_.js exists.

module.exports = () => {
    var MAIN = ['index']; // all controllers use /midware/index.js as their midware

    var user = ['user']; // controller files under controller/user dir use user.js as their midware

    var calculator = ['calcu1', 'calcu2']; // controller files under controller/calculator dir use calcu1.js and calcu2.js as their midware.

    // exports the indication
    return {
        MAIN,
        user,
        calculator
    };
}

surport alias

we can import a file use absolute dir as below.

var logger = require('@s/helper/logger');

hooks

open the abilities of the app, we can do something in the very beginning of the app with onStart callback. also do something when the app booted.

// called when app init
function onStart (app, router) {

}

// called when app booted
function onBooted () {

}

try {
    require('../www')(onStart, onBooted);
} catch (e) {
    console.log(`there isn't www file, please run "npm run sbuild"`);
};

that's all and thanks for your suggests 😁 and I hope you can try it and add some issues for me 😜