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

egg-router-plus

v2.0.0

Published

The missing router features for eggjs

Downloads

6,365

Readme

egg-router-plus

NPM version build status Test coverage David deps Known Vulnerabilities npm download

The missing router features for eggjs

Install

$ npm i egg-router-plus --save

Then mount plugin:

// {app_root}/config/plugin.js
exports.routerPlus = {
  enable: true,
  package: 'egg-router-plus',
};

Features

load app/router/**/*.js

this plugin will auto load router define at app/router/**/*.js.

Notice: all sub routers will be loaded before app/router.js, please ensure all the sub router definitions are not conflict(better to use app.router.namespace to create different namespaces for each sub router file).

app.router.namespace

app.router.namespace(prefix, ...middlewares);
  • prefix - {String}, the prefix string of sub router
  • middlewares - {...Function}, optional group middlewares

Support same as Router:

  • router.verb('path-match', app.controller.action);
  • router.verb('path-match', middleware1, ..., middlewareN, app.controller.action);
  • router.verb('router-name', 'path-match', app.controller.action);
  • router.verb('router-name', 'path-match', middleware1, ..., middlewareN, app.controller.action);

prefix and path are not allow to be regex, and prefix can't be '/'.

// {app_root}/app/router.js
module.exports = app => {
  const subRouter = app.router.namespace('/sub');
  // curl localhost:7001/sub/test
  subRouter.get('/test', app.controller.sub.test);
  subRouter.get('sub_upload', '/upload', app.controller.sub.upload);

  // const subRouter = app.router.namespace('/sub/:id');
  // const subRouter = app.router.namespace('/sub', app.middleware.jsonp());

  // output: /sub/upload
  console.log(app.url('sub_upload'));
};

Every different prefix will bind to different router instance, and all the namespaces will sort by trie tree to ensure best match.

module.exports = app => {
 const apiRouter = app.router.namespace('/api');
 const apiWebRouter = app.router.namespace('/api/web');
 const apiWebAdminRouter = app.router.namespace('/api/web/admin');
 apiRouter.get('/:a/:b/:c', controller.api.one);
 apiWebRouter.get('/:a/:b', controller.api.two);
 apiWebAdminRouter.get('/:a', controller.api.three);
 // /api/web/admin/hello => controller.api.three
 // /api/web/foo/hello => controller.api.two
 // /api/foo/bar/hello => controller.api.one
};

Known issues

  • sub redirect is not support, use app.router.redirect() or redirect to a named router.
const subRouter = app.router.namespace('/sub');

// will redirect `/sub/go` to `/anyway`, not `/sub/anyway`
subRouter.redirect('/go', '/anyway');

// just use router
router.redirect('/sub/go', '/sub/anyway');

// or redirect to a named router
subRouter.get('name_router', '/anyway', app.controller.sub.anyway);
// will redirect `/sub/go_name` to `/sub/anyway` which is named `name_router`
subRouter.redirect('/sub/go_name', 'name_router');

Questions & Suggestions

Please open an issue here.

License

MIT