0.3.2 • Published 5 years ago

mvkoa v0.3.2

Weekly downloads
1
License
AGPL-3.0
Repository
github
Last release
5 years ago

MVKoa

Node.JS back-end framework based on Koa 2 & ECMAScript Decorator proposal

NPM Dependency Build Status

NPM

Basic usage

Core logic

source/UserController.js

(source/User.js is a Model class)

import KoaController, { GET, PATCH, POST } from 'mvkoa';

import { User } from './User';

export default class UserController extends KoaController {
    @GET()
    listAll() {
        return [];
    }

    @PATCH('/:id')
    extend(context, id, body) {
        return { id, ...body };
    }

    @POST('/', User)
    create(context, body) {
        return body.valueOf();
    }
}

source/index.js

import KoaController from 'mvkoa';
import bodyParser from 'koa-bodyparser';
import mount from 'koa-mount';

import UserController from './UserController';

const app = new KoaController()
    .use(async (context, next) => {
        try {
            await next();
        } catch ({ message }) {
            (context.status = 500), (context.body = message);
        }
    })
    .use(bodyParser())
    .use(mount('/users', new UserController()));

app.listen(() => console.log(`Server run at ${app.address}`));

Installation

npm init

npm install \
    mvkoa \
    koa-bodyparser \
    koa-mount \
    data-scheme \
    @babel/polyfill \
    @babel/runtime

npm install \
    @babel/cli \
    @babel/core \
    @babel/preset-env \
    @babel/plugin-proposal-decorators \
    @babel/plugin-transform-runtime

Configuration

package.json

{
    "scripts": {
        "build": "babel source/ -d dist/ -s",
        "start": "node dist/"
    },
    "engines": {
        "node": "^7.6.0"
    },
    "babel": {
        "presets": [
            [
                "@babel/preset-env",
                {
                    "targets": {
                        "node": "7.6.0"
                    }
                }
            ]
        ],
        "plugins": [
            [
                "@babel/plugin-proposal-decorators",
                {
                    "decoratorsBeforeExport": true
                }
            ],
            "@babel/plugin-transform-runtime"
        ]
    }
}

Bootstrap

npm run build

npm start

Advanced usage

https://tech-query.me/MVKoa/manual/