0

I am writing an Express JS application and I need to calculate a route given a controller function or route path.

For example:

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}

Or, given parameters, it could fill in the URL parameters, for example:

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

Coming from .NET, this is pretty easy using UrlHelper:

new UrlHelper(Request).Link("Things Route Name", new { id = 1 }); // returns https://host/api/v1/things/1

James
  • 697
  • 5
  • 23

1 Answers1

0

Does req.originalUrl help in this regard?

motss
  • 662
  • 4
  • 6
  • I suppose that if I'm rolling my own solution, that would give me a way to determine the base URL (host, root path, etc). But I'm hoping to leverage Express if I can. – James May 10 '18 at 12:58