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

@cderwu/koa_autorouter

v1.3.1

Published

koa_autorouter

Downloads

8

Readme

koa_autorouter

基於 KOA 框架的路由系統

Purpose

以資料夾管理系統的情況,希望可以做到完全的封裝,資料夾存在即有效,移除資料夾該功能便消失且不影響其他功能。

Initial

server 啟動時引入模組

const autorouter = require('koa_autorouter');

開始讀取系統位置

autorouter.autoLoadBySystem(`${process.cwd()}/script/system`);

設定中介軟體

//必定需通過的 Middleware
autorouter.setRequireMiddleware({
    STOPWATCH: require('./middleware/stopwatch.js'),
    GETPARAM: require('./middleware/getparam.js'),
    INPUT: require('./middleware/input.js'),
    OUTPUT: require('./middleware/output.js'),
});
//可選擇須不須通過的 Middleware
autorouter.setOptionMiddleware({
    AUTHORITY: require('./middleware/authority.js'),
});

啟動路由

//此處的 app 為 koa 的 Instance
autorouter.launchRouter(app);

Using

${process.cwd()}/script/system路徑下的 routes 檔案

const autorouter = require('koa_autorouter');

// GET /test
exports.get_test = async function (ctx, next) {
    let param = ctx.request.query;
    ctx.body = `123 321 ${JSON.stringify(param)}`;
}

// POST /test
exports.post_test = async function (ctx, next) {
    let param = ctx.request.body;
    ctx.body = { result: 0, param, msg: '/test call success' };
}

// POST /hh/test
exports.post_hh_test = async function (ctx, next) {
    let param = ctx.request.body;
    ctx.body = `789 987 ${JSON.stringify(param)}`;
}

//未使用選用的中介軟體呼叫
autorouter.routerAdder(exports);

${process.cwd()}/script/system/testsys路徑下的 routes 檔案

const autorouter = require('koa_autorouter');
const middleWare = autorouter.getMiddleware();

// GET /testsys/test
exports.get_test = async function (ctx, next) {
    let param = ctx.request.query;
    ctx.body = `123 321 ${JSON.stringify(param)}`;
}
middleWare.AUTHORITY(exports);
//以下路由需要額外通過 AUTHORITY 中介軟體檢查
// POST /testsys/test
exports.post_test = async function (ctx, next) {
    let param = ctx.request.body;
    ctx.body = `321 123 ${JSON.stringify(param)}`;
}

// POST /testsys/hh/test
exports.post_hh_test = async function (ctx, next) {
    let param = ctx.request.body;
    ctx.body = `789 987 ${JSON.stringify(param)}`;
}

//有使用選用的中介軟體呼叫
autorouter.routerAdderWithMid(exports);
  • 以系統為名稱的資料夾,如果資料夾內有 routes.js,那 routes.js exports 的 function 會自動生成路由 EX: 系統資料夾 item/routes.js exports.get_use = function(ctx, next) 自動產生路由 'item/use'
  • function 名稱的命名規則為
`${methods}_${額外的類別(選用)}_${路由名稱}`