Creating the application controllers

The next step is to create the controls for the models User and Band:

  1. Within the controllers folder, create a new file called User.js and add the following code:
          var models = require('../models/index'); 
          var User = require('../models/user'); 
     
          // Create Users 
          exports.create = function(req, res) { 
              // create a new instance of the Users model with request body 
              models.User.create({ 
                name: req.body.name, 
                  email: req.body.email 
              }).then(function(user) { 
                  res.json(user); 
              }); 
           }; 
       
           // List Users 
           exports.list = function(req, res) { 
               // List all users 
               models.User.findAll({}).then(function(users) { 
                   res.json(users); 
              }); 
          }; 
    

    Tip

    Note that the first line of the file imports the index model; this file is the basis for creation of all the ...

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.