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