自动挂载路由

注意是next版本,我们用的是koa2

  1. $ npm install --save mount-koa-routes@next

app.js里修改如下

修改1

  1. var routes = require('./routes/index');
  2. var users = require('./routes/users');

改为

  1. const mount = require('mount-koa-routes');

修改2

  1. app.use('/', routes);
  2. app.use('/users', users);

改为

  1. mount(app, __dirname + '/routes', true);

然后

  1. $ npm start
  2. [nodemon] app crashed - waiting for file changes before starting...
  3. [nodemon] restarting due to changes...
  4. [nodemon] starting `node bin/www`
  5. koa deprecated Support for generators will been removed in v3. See the documentation for examples of how to convert old middleware https://github.com/koajs/koa/tree/v2.x#old-signature-middleware-v1x app.js:18:5
  6. mount route /index.js
  7. mount route /users.js
  8. ******************************************************
  9. MoaJS Apis Dump
  10. ******************************************************
  11. ┌───────────────────────────────────────────────────────┬────────┬─────────┐
  12. File Method Path
  13. ├───────────────────────────────────────────────────────┼────────┼─────────┤
  14. /Users/sang/workspace/github/your-koa/routes/index.js GET /
  15. ├───────────────────────────────────────────────────────┼────────┼─────────┤
  16. /Users/sang/workspace/github/your-koa/routes/users.js GET /users/
  17. └───────────────────────────────────────────────────────┴────────┴─────────┘

在路由里复制users.js为test.js

测试

http://127.0.0.1:3000/test

妈妈再也不用担心我写路由了

这样做是有好处也有坏处的。但均衡来讲,好处大于坏处,感谢koa-router的路由机制,让我们可以足够定制的里面的逻辑

如果你觉得自动路由不够用,你自己手动挂载也是可以的,只要你不嫌麻烦