Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think the correct way to do this is to implement <strong>content negotiation</strong> into your application. Yeah, Express 3.x facility is a way to do it and provides a direct answer to your question, but I don't think it's the best way to do it because it places the content negotiation responsibility in the routing logic. I don't think that's a good place for it because it doesn't follow the <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle" rel="nofollow">single responsibility</a> principle, having put the <strong>content negotiation</strong> in routing logic.</p> <p>I've taken a stab at implementing <strong>content negotiation</strong> in my <a href="https://github.com/ijoey/joeyguerra/blob/master/represent.js" rel="nofollow">blog engine</a>; reviewing that might help guide you in a good direction. The gist is that the code determines the file extension via <strong>content negotiation</strong> logic. Then, with the file extension, it wants to find the corresponding view file, renders it into a response and sends it back to the client. The idea is that it responds with the requested resource in the requested representation per the content negotiation. The <a href="https://github.com/ijoey/joeyguerra/blob/master/resources/index.js#L23" rel="nofollow">routing logic</a> only specifies a view, but has no idea about content negotiation. That happens outside of the routing logic which makes for a more flexible design.</p> <p>The result of this design is the ability to ask for a specific representation of the resource like:</p> <p><a href="http://blog.joeyguerra.com/index.json" rel="nofollow">http://blog.joeyguerra.com/index.json</a> and get a JSON representation <a href="http://blog.joeyguerra.com/index.phtml" rel="nofollow">http://blog.joeyguerra.com/index.phtml</a> and get partial (or an HTML fragment) HTML representation <a href="http://blog.joeyguerra.com/index.xml" rel="nofollow">http://blog.joeyguerra.com/index.xml</a> and get an XML representation.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload