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

koa-automate-router

v1.0.3

Published

结合 koa 和 koa-router 实现自动化路由

Downloads

2

Readme

简介:

结合 koa 框架和 koa-router 中间件, 编写的自动化路由插件 省去纷繁复杂的路由编写

安装和使用:

初始化一个 node 项目, 安装以下依赖

npm i koa

npm i koa-router

npm i koa-automate-router

创建 index.js 文件和 router 目录

初始化代码

// 引入外部依赖
const Koa = require('koa');
const autoRouter = require("koa-automate-router")

// 实例化一个服务器并监听一个端口
const app = new Koa();
app.listen(80);

// 调用自动化路由
autoRouter(__dirname + "/router", app);

业务逻辑代码格式

1, 我们在router目录下创建一个子目录 home

2, 在 home 目录下创建文件 test.js

3, test.js 的内容如下, 基本是 koa-router 的内容, 看不懂请先学习 koa-router

const Router = require("koa-router");
let router = new Router();

router.get("/home/test/get", async(ctx) => {
    ctx.body = "这里是 GET 请求";
});

router.post("/home/test/post", async(ctx) => {
    ctx.body = "这里是 POST 请求";
});

module.exports = router;

4, 如上, 我们就可以通过路由 localhost/home/test/get 或 localhost/home/test/post 访问到 test.js 中两个配置好的路由

4.1 /home/test/get 对应 router.get() 方法的 第一个参数

4.2 为了后期好维护 路由的命名最好使用 目录名/文件名/方法名 的方式

4.3 尽量避免路由出现重复的情况,扫描时从上到下,文件排序在前的优先级高

建议

router 目录值作为控制器, 具体的业务逻辑不要写在这个目录内