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

auto-router-koa

v1.0.6

Published

自动化路由, 帮你免去纷繁复杂的路由编写

Downloads

2

Readme

简介:

结合 koa 框架, 编写的自动化路由框架, 省去纷繁复杂的路由编写

安装和使用:

npm i auto-router-koa

index.js 文件的内容, 假设根目录下有 index.js 文件 server 目录

以下就把 server 作为控制器目录

// 引入自动化路由依赖
const Koa = require('koa');
const AutoRoute = require("auto-router-koa"); 
// 实例化 koa 
const app = new Koa();
app.listen(80);

// 载入自动化路由
// 第一个参数是 koa 的实例
// 第二个参数是业务逻辑 控制器 存放的目录位置( 也可以是相对路径 )
// 第三个参数为项目跟路径, 用于在 控制器中 引用外部模块使用
AutoRoute(app, __dirname + "/server", __dirname);

业务逻辑代码格式

我们在 server 目录下创建了 home 目录 并在 home 下创建了 index.js

index.js 的代码固定格式如下:

注意: 用 ctx.sendMsg 取代 ctx.body, 其它不变

new Promise(async (resolve, reject) => {
    // 所有代码写在该方法内, 包括引用外部模块 
    let fs = require("fs");   
    ctx.sendMsg("这里被 /home/index 路由访问");
});

以上代码不需要编写路由和任何其他代码, 就能在 http://localhost:8080/home/index 这个地址访问到

获取项目跟路径

new Promise(async (resolve, reject) => {
    // 项目根路径, 由初始化传递的第三个参数决定
    let rootPath = ctx.rootPath;
    console.log(ctx.rootPath);
});

无线递归

控制器目录下可以创建任意层级的 js 文件, 访问的路由就是该 js 文件相对 控制器目录 server 的相对路径

如: http://192.168.3.36/a/b/c/d 就是访问:

控制目录下的 a 目录下的 b 目录下的 c 目录下的 d.js