Skip to main content

Exercise & Assignment

1. Change Project Structure

image.png

ref: Project structure for an Express REST API when there is no "standard way" – Corey Cleary

  1. Create directory name routers , controllersand services 

    image.png

  2. Create a file in the router directory banks.js and users.js
    1. user.js file 
      const express = require("express");
      const userRouter = express.Router();
      userRouter.get("/", getUser);
      userRouter.get("/balance", getBalance);
      
      module.exports = userRouter;

    2. banks.js  file
      const express = require("express");
      const banksRouter = express.Router();
      bankRouter.get("/all", getHistory);
      bankRouter.get("/deposit", getDeposit);
      bankRouter.get("/withdraw", getWithdraw);
      bankRouter.get("/transfer", getTransfer);
      
      module.exports = banksRouter;
      

    3. index.js file
      const express = require("express");
      const app = express();
      const port = 3000;
      const userRouter = require("./router/users.js");
      const bankRouter = require("./router/banks.js");
      app.use(express.json());
      app.use(express.urlencoded({ extended: true }));
      
      app.get("/", (req, res) => {
        res.send("Hello World!");
      });
      app.use("/user", userRouter);
      app.use("/bank", bankRouter);
      
      app.listen(port, () => {
        console.log(`Example app listening on port ${port}`);
      });
      

  3. Folder services create db.js
    module.exports = data = require("../data.json");

  4. Create in folder Controller create directory users and banks

    image.png

      1. Create File getUser.js in user directory
        implement getUser function (return user by id)
        const data = require("../../service/db.js");
        const getUser = (req, res) => {
          //implement here
        };
        module.exports = getUser;
        
      2. create File getBalance.js
        implement getBalance function (return balance of user by id)

        const data = require("../../service/db.js");
        const getBalance = (req, res) => {
          //implement here
        };
        module.exports = getBalance;
      3. create File getHistory.js
        implement getHistory function (return all of the transactions by owner's ID)

        const data = require("../../service/db.js");
        const getHistory = (req, res) => {
          //implement here
        };
        module.exports = getHistory;

         

      4. create File deposit.js
        implement getDeposit function (return all transactions of type deposit by owner's ID)

        const data = require("../../service/db.js");
        const getDeposit = (req, res) => {
          //implement here
        };
        module.exports = getDeposit;
        
      5. create File deposit.js
        implement getDeposit function (return all transactions of type deposit by owner's ID)

        const data = require("../../service/db.js");
        const getDeposit = (req, res) => {
          //implement here
        };
        module.export = getDeposit;
        
      6. create File transfer.js
        implement getTransfer function (return all transactions of the type transfer by owner's ID)

        const data = require("../../service/db.js");
        const getTransfer = (req, res) => {
          //implement here
        };
        module.exports = getTransfer;
        
      7. create File withdraw.js
        implement getWithdraw function (return all transactions of type withdraw by owner's ID)

        const data = require("../../service/db.js");
        const getDeposit = (req, res) => {
          //implement here
        };
        module.exports = getDeposit;
        

2. Implement all files in the Controllers

3. Import function to router file 

  • banks.js

    image.png


  • user.js

    image.png


4. zip file without folder nodemodule and send to cscms

-------------------------------------------------------------------- End ----------------------------------------------------------------------