Coder Social home page Coder Social logo

egg-router-plus's Introduction

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

egg-router-plus's People

Contributors

atian25 avatar chs97 avatar daydayhappychao avatar dead-horse avatar fuxingzhang avatar jeff-tian avatar tporl avatar zhennann avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.