项目作者: kaelzhang

项目描述 :
Painless router definition for egg
高级语言: JavaScript
项目地址: git://github.com/kaelzhang/egg-define-router.git
创建时间: 2019-03-12T13:29:16Z
项目社区:https://github.com/kaelzhang/egg-define-router

开源协议:MIT License

下载


Build Status
Coverage



egg-define-router

Painless router definition for eggjs.

Install

  1. $ npm i egg-define-router

Usage

egg-define-router is used to define eggjs routers.

If we have an egg project with the following classic directory structure:

  1. egg-project
  2. |-- app
  3. | |-- router.js
  4. | |-- controller
  5. | | |-- foo.js
  6. | |-- middleware
  7. | | |-- custom
  8. | | | |-- clean.js
  9. | |-- ...
  10. |-- ...

And in router.js:

  1. const defineRouter = require('egg-define-router')
  2. const path = require('path')
  3. module.exports = defineRouter({
  4. // Equivalent to `'GET /foo/bar': 'foo.bar'`
  5. // -> app.router.get('/foo/bar', app.controller.foo.bar)
  6. '/foo/bar': 'foo.bar',
  7. 'POST /foo/bar': 'foo.bar',
  8. 'PUT /foo/baz': ['clean', 'foo.baz']
  9. }, {
  10. middlewareRoot: path.join(__dirname, 'middleware', 'custom')
  11. })

Config a basic router of GET method

If we have the following routes definition:

  1. {
  2. // If there is no method specified,
  3. // the method type defaults to `GET`
  4. '/foo/bar': 'foo.bar',
  5. 'POST /foo/bar': 'foo.bar'
  6. }

which is equivalent to:

  1. app.router.get('/foo/bar', app.controller.foo.bar)
  2. app.router.post('/foo/bar', app.controller.foo.bar)

With custom middlewares

  1. {
  2. 'PUT /foo/baz': [
  3. // `clean` is the middleware name
  4. 'clean',
  5. 'foo.baz'
  6. ]
  7. }

Which is equivalent to:

  1. app.router.put(
  2. '/foo/baz',
  3. require(`${middlewareRoot}/clean`),
  4. app.controller.foo.baz
  5. )

Directly define middleware/controller functions

  1. {
  2. 'DELETE /user': async ctx => {
  3. await deleteUser(ctx.params.id)
  4. ctx.body = 'deleted'
  5. },
  6. 'PUT /comment': [
  7. preventXSSMiddleware,
  8. 'comment.create'
  9. ]
  10. }

defineRouter(routes, {middlewareRoot})

  • routes Routes
  • middlewareRoot path The search path for middleware name.

Creates the router function which accept one argument app.

routes

The complete definition of routes lists below.

  1. type KoaFunc = (ctx: KoaContext, next?: KoaNext) => any
  2. type ControllerName = string
  3. type MiddlewareName = string
  4. type Controller = ControllerName | KoaFunc
  5. type Middleware = MiddlewareName | KoaFunc
  6. type RouteDefinition = Controller
  7. | [...Array<Middleware>, Controller]
  8. interface Routes {
  9. [methodAndPathname: string]: RouteDefinition
  10. }

Here is an example for better understanding:

  1. routes: {
  2. 'pathname': controllerFunction,
  3. 'METHOD pathname': 'controllerName',
  4. 'GET pathname': [
  5. 'middlewarename',
  6. middlewareFunction,
  7. ...,
  8. 'controllerName'
  9. ],
  10. 'DELETE pathname': [
  11. 'middlewarename',
  12. middlewareFunction,
  13. ...,
  14. controllerFunction
  15. ],
  16. }

License

MIT