url_parse.js 944 B

123456789101112131415161718192021222324252627
  1. 'use strict';
  2. // url类
  3. const Url = require('url');
  4. module.exports = options => {
  5. /**
  6. * 控制器以及动作的获取
  7. *
  8. * @param {function} next - 中间件继续执行的方法
  9. * @return {void}
  10. */
  11. return function* urlParse(next) {
  12. this.actionName = '';
  13. // 获取当前控制器和动作名称
  14. const urlInfo = Url.parse(this.request.originalUrl, true);
  15. const url = urlInfo.pathname.substr(1);
  16. const actionInfo = url.split('/');
  17. this.controllerName = typeof actionInfo === 'object' && actionInfo[0] !== undefined ? actionInfo[0] : '';
  18. this.actionName = typeof actionInfo === 'object' && actionInfo[1] !== undefined ? actionInfo[1] : '';
  19. this.urlInfo = urlInfo;
  20. // 防止为空
  21. this.request.headers.referer = this.request.headers.referer === undefined ? '/dashboard' : this.request.headers.referer;
  22. yield next;
  23. };
  24. };