Creating a controllers folder

  1. Create a folder called controllers inside the root project folder.
  2. Create an index.js inside the controllers folder and place the following code:
          // Index controller 
          exports.show = function(req, res) { 
          // Show index content 
              res.render('index', { 
                  title: 'Express' 
              }); 
          }; 
    
  3. Edit the app.js file and replace the original index route app.use('/', routes); with the following code:
          app.get('/', index.show); 
    
  4. Add the controller path to the app.js file right after var swig = require('swig'); declaration, replace the original code with the following code:
          // Inject index controller 
          var index = require('./controllers/index'); 
    
  5. Now it's time to check if all goes as expected: we'll run the application and check the result. Type in your ...

Get Node.js 6.x Blueprints now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.