Controllers making guide OLD (POST works with MongoDB)

  1. getItem by id
    • Case when we get correct id
      • in route means router.get('/:id', controller.getById);
      • in controller that parameters are in req.params object
      • so in controller f.e. use method find

        See the Pen 5 - get Item by id by Andrii (@imitator) on CodePen.

        Remember about id in databases, ofter set  ̲ id, so method find will be
        const oneProduct = products.find(item => item._id === id);
    • If we get incorrect id
  2. POST (We make it in cash without real db/rewrite in to file)
    • Make route
      • routes/products.js

        router.post('/', products.add);

    • Create controller
      • controllers/products/add.js

        Firstly for testing what "req.body" is, we get it in console

        For converting body req we should use "express.json()" middleware. In our example it used for all requests by setting it in server.js by app.use(express.json());.

        But for optimizing our server we may use it only for body requests, that's mean in routes/products.js

        router.post('/', express.json(), products.add);

        Before express.json() developers used another middleware building in node.js, called body-parser , in server.js we may use it.
      • Make request via postman
      • For testing use UUID to generate id-s

        npm i uuid

    • To sum up, first version of add.js controller (in this case we add product in cash, NOT to data/products.js file)

      See the Pen 9 - POST controller by Andrii (@imitator) on CodePen.

    Controller POST works with MongoDB

    • Make the simplest Schema in models/schemas/product.schema.js

      Schema below 👇🏼 is really simplest, b/c it's more like recommendation, than require fields, so go to Schema guide

      See the Pen 17 - Schema (for work with MongoDb) by Andrii (@imitator) on CodePen.

      Remember about reexport from folder in models/schemas/index.js
      const productSchema = require('./product.schema');
      module.exports = {
      productSchema,
      };
    • Use Schema in Model in models/product.model.js
      const { model } = require('mongoose');
      const { productSchema } = require('./schemas');
      const Product = model('product', productSchema);
      module.exports = Product;

      Remember about reexport from models folder in models/index.js

      const Product = require('./product.model');
      module.exports = {
      Product,
      };
    • Refactor ctrl from local file to work with MongoDB

      See the Pen 18 - POST ctrl using for MongoDB by Andrii (@imitator) on CodePen.

      Some errors aren't in list of http-status-codes, so we try to handle them in catch(error){}, other errors we gather in other place by sending them in next(error), in app.js(index.js), this will be explaining in next Catching errors guide

  3. UPDATE (We make it in cash without real db/rewrite in to file)
    • Create route

      router.put('/:id', express.json(), products.update);

      P.S. express.json - middleware for body, could be use in server.js, like app.use(express.json()); for all requests, the same explanation was in POST method

    • Create controller

      See the Pen 10 - UPDATE controller by Andrii (@imitator) on CodePen.

  4. DELETE (We make it in cash without real db/rewrite in to file)

    P.S. It is impossible to create "delete" controller name, b/c it is reserved word.