title: Using View Models in Node.js App
body: <p>I'm learning Node.js. At this time, I am using ES6, Node, and Express. I am concerned about the scope of my view model. At this time, I have the following:</p>
  
  <p><strong>server.js</strong></p>
  
  <pre><code>// setup the routing
  var routes = require('./routes');
  app.use('/', routes.home);
  </code></pre>
  
  <p>The routes are properly registering. I have my routes defined in $projectDir/routes/home.js. That file looks like this:</p>
  
  <p><strong>home.js</strong></p>
  
  <pre><code>var router = require('express').Router();
  
  /* GET the main page. */
  router.get('/', function(req, res) {
      var viewModel = require('../viewModels/home/index');
      res.render('home/index', viewModel);
  });    
  
  /* GET the contact page. */
  router.get('/contact', function(req, res) {
    var viewModel = require('../viewModels/home/contact');
    res.render('home/contact', viewModel);
  });
  
  /* POST contact information */
  router.post('/contact', function(req, res) {
    // ?
  });
  </code></pre>
  
  <p>That approach works just fine. I've defined my view models at $projectDir/viewModels/home/index.js and $projectDir/viewModels/home/contact.js.</p>
  
  <p><strong>index.js</strong></p>
  
  <pre><code>var viewModel = {
      title: '',
  
      constructor() {
          this.title = 'Welcome';     
      }
  };
  module.exports = viewModel;
  </code></pre>
  
  <p><strong>contact.js</strong></p>
  
  <pre><code>var viewModel = {
      title: '',
      emailAddress: '',       
  
      constructor() {
          this.title = 'Contact Us';      
      }
  };
  module.exports = viewModel;
  </code></pre>
  
  <p>I have concerns around the scope of view model. First, I'm using <code>module.exports = viewModel;</code> in two separate files. Is this ok? Or should I name the variables something like <code>indexViewModel</code> and <code>contactViewModel</code>? Also, when a POST happens, how do I populate use the view model with the values that the user entered into a form? For example their email address?</p>