I am trying to build a system that uses nodejs with express to render html pages based on data from another backend service. The backend service provides JSON data that the nodejs server uses to render the html and then pass to the browser. Having read up on REST and the Hyemedia constraint I want to use this in the design, but can not seem to figure it out. A simple example would be a page that provides a list of items in which each item is a link that can be clicked on to get the full details of that item. So the backend service might make some JSON that looks like
{
"title": "List of items"
"self": "http://api.hostname.com/items
[
{
"title":"item1",
"url": "http://api.hostname.com/items/1
}
{
"title":"item2",
"url": "http://api.hostname.com/items/2
} {
"title":"item3",
"url": "http://api.hostname.com/items/3
}
}
I then have nodejs render a page in html with a list of links using “title” for the name. The issue is then I don’t know what to use for the href. It would work if the href is to a view that with render the item like
href="/items/2
but I have no way of pass the “url” field back to node so node can pass that to the backend service. I end up building the url for the backend service based on the view url. Which if I understand REST is not the Hypermedia way. It means my node code has to know the format of the urls of the backend service instead of getting the urls and using directly.
So is there so clever feature like adding the “url” field as a query parameter of the href to preserve it on the client? I am misunderstanding the Hypermedia Constraint or should I being trying to use it at all?
Whether you should use the hypermedia constraint is a matter of opinion. (I have a blog post stating my own opinion).
As a more specific answer, I’ll point out that you actually have 2 separate REST systems in this application, one of which is serving html and one of which is serving json. If they are equivalent in functionality, the hypermedia constraint would expect that they actually be a single system, where requests could return different media types based on the Accepts http header. This eliminates your problem (because the urls become the same).