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

exp-router

v1.0.0

Published

express router middleware which simplify express route config

Downloads

13

Readme

exp-router

express路由中间件。中间件自动获取指定目录文件夹中定义的路由,并简化路由配置形式。

安装

全局安装或者本地安装:

npm install -S exp-router

中间件使用方法

const path = require('path');
const express = require('express');
const expressRouter = require('exp-router');

const app = express();
// 指定项目目录下routes目录作为路由目录
const routesPath = path.resolve(__dirname, './routes');


// 启用路由中间件
app.use(expressRouter(routesPath));

// ...省略express其它代码

路由定义格式

module.exports = [
    [[sortNum], method, path, callback [, callback ...] ],
    [[sortNum], method, path, callback [, callback ...] ],
    ...
]
  • sortNum Number 可选参数,用来定义路由的声明顺序,值越小,路由声明顺序越靠前
  • method String 必须参数,用来声明路由的method, 不区分大小写('all', 'get', 'post',...),具体参考express method定义
  • path [String/RegExp/...] 必须参数,路由的路径,支持express所有path配置方式
  • callback 路由处理方法(可以是中间件),用法与express callback相同

路由写法示例

// 假定使用了./routes/作为路由目录
// ./routes/test.js中声明路由

/**
 * 路由处理方法1
 */
const testAction1 = (req, res, next) => {
    console.log('test action1');
}

/**
 * 路由处理方法2
 */
const testAction2 = (req, res, next) => {
    res.status(200).status('ok');
}


/**
 * routes配置,配置格式如下:
 * routes = [
 *     ['get', '/abc', fun1, [fun2, fun3, ...]],
 *     ['post', '/abcd', fun1, fun2],
 *     ['GET', '/ab/:params1/:params2', fun1],
 *     ...
 * ]
 */
module.exports = [
    // 音频测试页
    [1, 'get', '/te', testAction2],
    [0, 'get', '/tes', testAction1],
    ['GET', '/test', testAction1, testAction2],
];