Skip to content

MVC Pattern in Node

Aakash Goplani edited this page Feb 21, 2020 · 7 revisions

Topics Covered


What is MVC

MVC pattern in Node


Flow of Request

  • You create a route that handles certain request

    router.get('/', (req, res, next) => {...});
  • You create a model that holds your data object that is to be presented on UI or used for internal logic processing. Model is basically a class with getter-setter to access properties of class.

  • You create a controller corresponding to your router i.e. if you have 5 route-paths then you have to create 5 different controllers to handles those requests. If you have configuration where multiple routes needs same action then you can assign a common controller.

    • Controllers are basically middlewares. We replace the middleware logic earlier defined in routes with Controllers. this is the example of controller:
      const Product = require('../models/product');
      ...
      exports.getProducts = (req, res, next) => {
         res.render('shop/product-list', { 
            prods: products, 
            title: 'All Products', 
            path: '/'
         });
      };
    • We export our controllers, so that they can be later imported (mostly by router)
    • We pass the render logic to the Controller that router used to handle previously.
    • Here we can access data from Model
    • And finally we display data to View stored in shop/product-list directory
  • Finally assign your controller to Router

    const shopController = require('../controllers/shop');
    ...
    router.get('/', shopController.getProducts);
    • Important - that you just pass the method reference and don't execute it (getProducts())

Reading a file

const filePath = path.join(rootDirectory, 'path_to_file');
E.G.
const filePath = path.join(rootDirectory, 'data', 'products.json');
  • Simple function:
fileSystem.readFile(filePath, (error, fileContents) => {
   if (error) {
      // handle error
   } else {
      // contents of file ready
   }
});
  • First argument is to specify the path of file

  • Second argument is the function that returns:

    • error
    • file contents
  • Since this is async function, it does not return values instantaneously, so we need to wrap this in callback

const getProductsFromFile = (callback) => {
    fileSystem.readFile(filePath, (error, fileContents) => {
        if (error) {
            callback([]);
        } else {
            callback(JSON.parse(fileContents));
        }
    });
}
save() {
   getProductsFromFile(products => {
      products.push(this);
      fileSystem.writeFile(filePath, JSON.stringify(products), (error) => {
         if (error) {
            // process error
          }
      }); 
   });
}

Writing in a file

fileSystem.writeFile(filePath, JSON.stringify(products), (error) => {
   if (error) {
      // process error
   }
}); 
  • First argument is to specify the path of file
  • Second argument is the data to be written to file
  • Third argument is error function to be handled
Clone this wiki locally