【发布时间】:2018-10-20 17:47:31
【问题描述】:
我正在编写一个 Express JS 应用程序,我需要计算一个给定控制器函数或路由路径的路由。
例如:
let myUrl = functionToGetRoutePath(ThingsController.getThing); // would return https://host/api/v1/things/{id}
// Or
let myUrl = functionToGetRoutePath('/things/{id}'); // would return https://host/api/v1/things/{id}
或者,给定参数,可以填写URL参数,例如:
let myUrl = functionToGetRoutePath(ThingsController.getThing, { id: 1 }); // would return https://host/api/v1/things/1
// Or
let myUrl = functionToGetRoutePath('/things/{id}', { id: 1 }); // would return https://host/api/v1/things/1
来自 .NET,使用 UrlHelper 非常简单:
new UrlHelper(Request).Link("Things Route Name", new { id = 1 }); // returns https://host/api/v1/things/1
【问题讨论】:
标签: express routing express-router