helper.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. 'use strict';
  2. const url = require('url');
  3. module.exports = {
  4. /**
  5. * Generate URL path(without host) for route. Takes the route name and a map of named params.
  6. * @function Helper#pathFor
  7. * @param {String} name - Router Name
  8. * @param {Object} params - Other params
  9. *
  10. * @example
  11. * ```js
  12. * app.get('home', '/index.htm', 'home.index');
  13. * ctx.helper.pathFor('home', { by: 'recent', limit: 20 })
  14. * => /index.htm?by=recent&limit=20
  15. * ```
  16. * @return {String} url path(without host)
  17. */
  18. pathFor(name, params) {
  19. return this.app.router.url(name, params);
  20. },
  21. /**
  22. * Generate full URL(with host) for route. Takes the route name and a map of named params.
  23. * @function Helper#urlFor
  24. * @param {String} name - Router name
  25. * @param {Object} params - Other params
  26. * @example
  27. * ```js
  28. * app.get('home', '/index.htm', 'home.index');
  29. * ctx.helper.urlFor('home', { by: 'recent', limit: 20 })
  30. * => http://127.0.0.1:7001/index.htm?by=recent&limit=20
  31. * ```
  32. * @return {String} full url(with host)
  33. */
  34. urlFor(name, params) {
  35. return this.ctx.protocol + '://' + this.ctx.host + url.resolve('/', this.app.router.url(name, params));
  36. },
  37. };