title: Building enterprise app with Node/Express
body: <p>I'm trying to understand how to structure enterprise applciation with Node/Express/Mongo (actually using MEAN stack). </p>
  
  <p>After reading 2 books and some googling (including similar StackOverflow questions), I couldn't find any good example of structuring large applications using Express. All sources I've read suggest to split application by following entities: </p>
  
  <ul>
  <li>routes</li>
  <li>controllers</li>
  <li>models</li>
  </ul>
  
  <p>But the main problem I see with this structure is that controllers are like god objects, they knows about <code>req</code>, <code>res</code> objects, responsible for validation and <em>have business logic</em> included in. </p>
  
  <p>At other side, routes seems to me like over-engineering because all they doing is  mapping endpoints(paths) to controller methods.</p>
  
  <p>I have Scala/Java background, so I have habit to separate all logic in 3 tiers - controller/service/dao. </p>
  
  <p>For me following statements are ideal: </p>
  
  <ul>
  <li><p>Controllers are responsible only for interacting with WEB part, i.e. marshalling/unmarshalling, some simple validation (required, min, max, email regex etc); </p></li>
  <li><p>Service layer (which actually I missed in NodeJS/Express apps) is responsible only for business logic, some business validation. Service layer doesn't know anything about WEB part (i.e. they can be called from other place of application, not only from web context);</p></li>
  <li><p>Regarding to DAO layer is all clear for me. Mongoose models are actually DAO, so it most clear thing to me here.</p></li>
  </ul>
  
  <p>I think examples I've seen are very simple, and they shows only concepts of Node/Express, but I want to look at some real world example, with much of the business logic/validation involved in. </p>
  
  <p>EDIT: </p>
  
  <p>Another thing isn't clear to me is absent of DTO objects. Consider this example: </p>
  
  <pre><code>const mongoose = require('mongoose');
  const Article = mongoose.model('Article');
  exports.create = function(req, res) {
      // Create a new article object
      const article = new Article(req.body);
      // saving article and other code
  }
  </code></pre>
  
  <p>There JSON object from <code>req.body</code> is passed as parameter for creating Mongo document. It smells bad for me. I would like to work with concrete classes, not with raw JSON</p>
  
  <p>Thanks.</p>