concept homeController.js in category node

appears as: homeController.js, homeController.js
Get Programming with Node.js

This is an excerpt from Manning's book Get Programming with Node.js.

To follow the MVC design pattern, move your callback functions to separate modules that reflect the purposes of those functions. Callback functions related to user account creation, deletion, or changes, for example, go in a file called usersController.js within the controllers folder. Functions for routes that render the home page or other informational pages can go in homeController.js by convention. Figure 9.2 shows the file structure that your application will follow.

  • Move your route callback functions to the home controller, and add them to that module’s exports object. Your route to respond with a vegetable parameter, for example, can move to your home controller to look like listing 9.6. In homeController.js, you assign exports.sendReqParam to the callback function. sendReqParam is a variable name, so you can choose your own name that describes the function.
    Listing 9.6. Moving a callback to homeController.js
    exports.sendReqParam = (req, res) => {              1
      let veg = req.params.vegetable;
      res.send(`This is the page for ${veg}`);
    };
    • 1 Create a function to handle route-specific requests.
  • Listing 9.6. Moving a callback to homeController.js
    exports.sendReqParam = (req, res) => {              1
      let veg = req.params.vegetable;
      res.send(`This is the page for ${veg}`);
    };