Exercise & Assignment
1. Change Project Structure
ref: Project structure for an Express REST API when there is no "standard way" – Corey Cleary
- Create directory name
routers
,controllers
andservices
- Create a file in the router directory
banks.js
andusers.js
users.js
file
const express = require("express"); const userRouter = express.Router(); userRouter.get("/", getUser); userRouter.get("/balance", getBalance); module.exports = userRouter;
banks.js
file
const express = require("express"); const bankRouter = express.Router(); bankRouter.get("/all", getHistory); bankRouter.get("/deposit", getDeposit); bankRouter.get("/withdraw", getWithdraw); bankRouter.get("/transfer", getTransfer); module.exports = banksRouter;
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}`); });
- Folder services create
db.js
module.exports = data = require("../data.json");
- Create in folder Controller create directory users and banks
-
- 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;
-
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;
-
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;
-
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;
-
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;
-
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;
-
create File
withdraw.js
implement getWithdraw function (return all transactions of type withdraw by owner's ID)const data = require("../../services/db.js"); const getDeposit = (req, res) => { //implement here }; module.exports = getDeposit;
- Create File
-
2. Implement all files in the Controllers
3. Import function to router file
4. zip file without folder nodemodule and send to cscms
-------------------------------------------------------------------- End ----------------------------------------------------------------------
No Comments