npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

brick-koa

v0.2.1

Published

koa with brick-engine

Downloads

2

Readme

brick-koa

基于brick-enginekoa框架适配工具包.用于适配使用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

License

MIT