-1

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?

Dmitry
  • 1
  • 1
  • 1
    There's no "correct" method for organizing methods in any language. What you'll get are opinions without and objective facts. Therefore this question is off-topic. – Heretic Monkey Sep 16 '16 at 17:25
  • Mike, I updated question with real world example. I know there may be many ways and code will work anyway, but what is professional way? I need a best practices advice to have a clean code here. – Dmitry Sep 16 '16 at 17:35
  • hard to understand much of this also – charlietfl Sep 16 '16 at 18:40
  • Anyone???? Can anyone help me pls? I am looking for best practices advice on this... – Dmitry Sep 17 '16 at 19:27

1 Answers1

0

Okay guys, looks like current stackoverflow community is useless, but i found the response to my question in previous posts here: in express.js, any way to capture request to both json and html in one function?

Community
  • 1
  • 1
Dmitry
  • 1
  • 1