I just started learning MEAN and creating my first project using it.
I am implementing MVC model with Node JS and Express and it works great.
The problem is that I have several callers for methods with the same data - from API, directly in web, and from angular in web and mobile. And they expect different data format - some pure HTML while others JSON.
The question is how to correctly organize methods?
With code duplication:
Function A () ( return rendered html )
Function AfromAPIorAJAX () ( return JSON )Or maybe have extra parameter FORMAT and use it in router:
Function A (format) ( if format == HTML return HTML else return JSON )Or try to identify caller accept type via XHR (not sure if it will work for API)?:
Function A () ( if req.xhr return JSON else return HTML )
What can you recommend?
How you organize the methods in the big MEAN projects?
Thanks.
UPDATE: Real world example:
I have model Article. Its controller have methods: create, list, delete etc.
I want to have page on website with route "/articles" that list all recent articles. That page must also have categories filter block - that if clicked it update articles list via AngularJS for articles from that category (of course this all can be done with angular only, but I want a page with default articles available and easily be indexed by google, and use angular just to update articles on the fly).
I want also to have route "/api/articles" that will return list of articles in JSON format for GET request (this route btw maybe also used by angular js filter above).
So the question how many methods I need to create for this purpose in Article controller - "list" and "renderList", or just "list(format)", or "list" that will look for xhr (again dunno if API clients send xhr), or any other yours variant?