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

kf-router

v2.2.2

Published

A File-based ReSTful router for koa.

Downloads

37

Readme

Koa 全自动文件路由 kf-router

Made with <3 by rikumi, Herald Studio

介绍

kf-router 是一个极简的、在 Koa 上运行的全自动文件路由中间件。只需在主程序中一行代码,即可根据需要的 ReSTful 路由结构,快速开始书写路由处理程序。

用法

安装

npm install -S kf-router

引入

kf-router 只需在 Koa 主程序的 app 中引入一个中间件:

//: app.js

const koa = require('koa')
const app = new koa()
const kf = require('kf-router')

app.use(kf())
app.listen(3000)

路由处理程序

kf-router 将 Koa 的中间件语法进行了简单改造,使得路由处理程序更易书写。

路由处理程序默认情况下只需放在主程序同级的 routes 文件夹内,并导出 exports.route 对象。

//: routes/hello.js

exports.route = {
    get() {
        return { hello: 'world!' }
    }
}

路由处理程序支持 get post put delete 等对应于 Koa ctx.request.method 所有可能值的、以小写字母命名的方法;它们可以是 async 方法,也可以是普通方法,均可以使用 return 直接向请求者返回内容。

若要访问 Koa ctx 对象,使用 this 即可:

//: routes/hello.js

exports.route = {
    async get() {
        return this.headers
    }
}

因为路由中间件将作为中间件栈的最顶层,next() 方法不再提供。请始终将 app.use(kf()) 放在主程序中所有 use 语句的最后,因为你无法使用路由以后的任何中间件。

路由处理程序可以放在任意一级子文件夹中,按照需要被请求的路径来命名,例如 a/b.js 可以处理 /a/b 的请求。路径可以为大小写字母、数字、下划线和中划线,不支持在路径名中使用参数。我们建议用原生的参数表来传递请求参数。

如果你既需要当前路径下包含子路由,又需要解析当前路径本身,可以用 index.js 来代替当前路径本身的路由处理程序。例如 /api/index.js 文件可以处理 /api 的请求,同时当然也可以处理 /api/index 的请求。

kf-router 会自动支持 koa-bodyparser,将请求参数(合并了 URL 参数和 body 参数)传入路由处理程序:

//: routes/add.js

exports.route = {
    async post({ a, b }) {
        return Number(a) + Number(b)
    }
}

注:若没有 koa-bodyparser,将只解析 URL 参数;若 body 为纯字符串,将只提供此字符串本身作为参数。

选项

kf() 支持传入一个参数来自定义路由处理程序所在的目录,默认为 routes

声明

kf-router 是简易的无协议开源软件,您暂时可以按照 WTFPL 随意使用它们。

kf-router 不是稳定有保障的软件,切勿在正式生产环境使用。