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 🙏

© 2025 – Pkg Stats / Ryan Hefner

express-router-maker

v1.0.11

Published

在RESTful风格中,可以简化express的router的书写,同时也更适应函数式编程的风格。

Downloads

29

Readme

express-router-maker

可以在RESTful风格中,简化express的router的书写,同时也更适应函数式编程的风格。

Router Maker

Example

const makeRouter = require('express-router-maker')

const routerMap = {
  GET: {  // or get
    '/GET_Path': (arg1, arg2) => {
      // do sth
      return sth;
    }
  },
  POST: { // or post
    '/POST_Path': POSTHandleFunction
  }
}

const router = makeRouter(routerMap)

我们只需要像上面这样,写HTTP方法、路径和路径对应的处理函数(handle function)。 通过makeRouter,会等效于下面这些

const express = require('express')
const router = express.Router()

router.get('/GET_Path', (req, res) => {
  // `处理函数`中的`参数`(arg1, arg2) 会自动地从`req.query`或者`req.body`中获取同名的值
  // 然后用这些参数执行`处理函数`
  // 最后用res.json返回执行 `处理函数` 的结果
})
router.post('/POST_Path', (req, res) => {
  // 同上
})

Description

源码在 src/router-maker.js

该工具可以根据一个特定对象routerMap(见Example的第一段代码)生成对应的router, 从而简化 router 的书写。

routerMap的属性名只能是HTTP 方法,比如GETPOST等,而routerMap的属性值同样都是对象。这些对象就表示具体的路由,以路径为Name、函数为Value

其中每个函数的参数将会从requestquery或者body中提取,每个函数的返回值,将会被res.json()发送。

发生错误时,会自动使用res.fail()发送错误信息。(见下一节的Response Decorator

Response Decorator

源码在 src/response-decorator.js

这其实算是一个express的中间件

用于向response添加一下函数:

  • success

    // 发送成功数据
    res.success = (data = {}) => {
      res.json({ success: true, ...data })
    }
  • fail

    // 返回失败信息
    res.fail = (error = '') => {
      res.json({ success: false, error })
    }
  • tryOrFail:

    尝试运行fn, fn可以是同步函数,异步函数,以及Promise。

    发生错误时,会自动返回请求。并进行输出

    res.tryOrFail = async (fn) => {
      try {
        //运行fn
      } catch (e) {
        res.fail()
      }
    }
  • tryOrFailSynctryOrFail的同步版本