brick-koa
v0.2.1
Published
koa with brick-engine
Downloads
2
Maintainers
Readme
brick-koa
基于brick-engine的koa框架适配工具包.用于适配使用koa框架进行http项目开发.
Install
npm install --save brick-koa
Usage
Setup 安装启用插件
const {defineApplication} = require('brick-engine');
const {koaSetup} = require('brick-koa);
const app = {};
koaSetup(app);
defineApplication(exports, app);
Koa Server 定义koa服务
const {defineKoa} = require('brick-koa);
const { KOA_SERVER_ID,ERROR_ID,COMPRESS_ID,STATIC_ID,ROUTER_ID } = require('./constants');
defineKoa(app, { key: KOA_SERVER_ID, deps: [
{ id: ERROR_ID },
{ id: COMPRESS_ID },
{ id: STATIC_ID },
{ id: ROUTER_ID },
] });
Static 定义静态文件(koa-static)中间件
const path = require('path');
const {defineStatic} = require('brick-koa);
const { STATIC_ID } = require('./constants');
defineStatic(app, { id: STATIC_ID, root: path.join(__dirname, 'public') });
BodyParser 定义koa-bodyparser中间件
const {defineBodyParser} = require('brick-koa);
const { BODYPARSER_ID } = require('./constants');
defineBodyParser(app, { id: BODYPARSER_ID });
Multer 定义文件上传(koa-multer)中间件
const path = require('path');
const {defineMulter} = require('brick-koa);
const { MULTER_ID } = require('./constants');
defineMulter(app, { id: MULTER_ID, dest: path.join(__dirname, 'temp'), single: 'file' });
Override 定义koa-override中间件
const {defineOverride} = require('brick-koa);
const { OVERRIDE_ID } = require('./constants');
defineOverride(app, { id: OVERRIDE_ID });
Error 定义koa-error中间件
const {defineError} = require('brick-koa);
const { ERROR_ID } = require('./constants');
defineError(app, { id: ERROR_ID });
Compress 定义koa-compress中间件
const {defineCompress} = require('brick-koa);
const { COMPRESS_ID } = require('./constants');
defineCompress(app, { id: COMPRESS_ID });
Router 定义路由(koa-router)中间件
const {defineRouter} = require('brick-koa);
const { ROUTER_ID,OVERRIDE_ID } = require('./constants');
defineRouter(app, { id: ROUTER_ID, deps: [{ id: OVERRIDE_ID }], opts: { prefix: '/simple' } });
RouteRule 定义路由处理方法
const { defineRouteRule } = require('brick-koa');
const { defineProviderFactory } = require('brick-engine');
const { ROUTER_ID, MULTER_ID, BODYPARSER_ID } = require('./constants');
const { Service } = require('./service');
class Router {
/**
* 构造函数
* @param {Service} service 服务
*/
constructor(service) {
this.service = service;
}
get(ctx) {
const res = this.service.find(ctx.query, ctx.params);
ctx.status = 200;
ctx.body = res;
}
async post(ctx) {
const res = await this.service.create(ctx.request.body);
ctx.status = 201;
ctx.body = res;
}
async put(ctx) {
const res = await this.service.update(ctx.query, { ...ctx.request.body, ...ctx.params });
ctx.status = 200;
ctx.body = res;
}
async upload(ctx) {
const { originalname, size } = ctx.req.file;
ctx.status = 200;
ctx.body = { filename: originalname, size };
}
}
exports.Router = Router;
// 定义get请求处理方法
defineRouteRule(Router, { id: ROUTER_ID, path: '/:id', method: 'get', component: Router, action: 'get' });
// 定义post请求处理方法
defineRouteRule(Router, { id: ROUTER_ID, deps: [{ id: BODYPARSER_ID }], method: 'post', component: Router, action: 'post' });
// 定义put请求处理方法
defineRouteRule(Router, { id: ROUTER_ID, deps: [{ id: BODYPARSER_ID }], path: '/:id', method: 'put', component: Router, action: 'put' });
// 定义文件上传post请求处理方法
defineRouteRule(Router, { id: ROUTER_ID, deps: [{ id: MULTER_ID }], method: 'post', path: '/upload', component: Router, action: 'upload' });
defineProviderFactory(Router, { deps: [{ id: Service }] });
Documentations
使用jsdoc
生成注释文档
git clone https://github.com/kiba-zhao/brick-koa.git
cd brick-koa
npm install
npm run docs
open docs/index.html