【问题标题】:Given a controller function or route path, how do I get the full route URL in Express JS?给定一个控制器函数或路由路径,如何在 Express JS 中获取完整的路由 URL?
【发布时间】: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


    【解决方案1】:

    req.originalUrl 在这方面有帮助吗?

    【讨论】:

    • 我想如果我正在推出自己的解决方案,那将为我提供一种确定基本 URL(主机、根路径等)的方法。但如果可以的话,我希望利用 Express。
    猜你喜欢
    • 2014-12-14
    • 2016-04-11
    • 1970-01-01
    • 2021-06-17
    • 2012-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多