We're going to make a controllers folder to handle the business logic for our routes separately, same as bootstraps
and iam
create a new folder under /todo
and name it controllers
and under controllers create a file called main.server.controller.js
modules└── todo├── bootstraps│ └── main.server.bootstrap.js├── iam│ └── main.server.iam.js└── controllers└── main.server.controller.js
And to create a new controller, you could use the built-in snippet.
Type ctrl
and press TAB
and it'll generate the controller's base code and JSDoc for you
modules/todo/controllers/main.server.iam.js
1/**2*3* @controller List4* @param {Express.Request} req The request5* @param {OutcommingMessage} res The response6* @param {Function} next Go to the next middleware7*/89exports. = async function (req, res, next) {1011};
Now let's create all the controllers for our CRUD
modules/todo/controllers/main.server.iam.js
1/**2 * List todos3 * @controller List4 * @param {Express.Request} req The request5 * @param {OutcommingMessage} res The response6 * @param {Function} next Go to the next middleware7 */89exports.list = async function list(req, res, next) {10 res.json({ message: "hello world" });11};1213/**14 * Create todo15 * @controller Create16 * @param {Express.Request} req The request17 * @param {OutcommingMessage} res The response18 * @param {Function} next Go to the next middleware19 */2021exports.create = async function create(req, res, next) {22 res.status(201).json({ message: "Todo Created" });23};2425/**26 * List one Todo27 * @controller One28 * @param {Express.Request} req The request29 * @param {OutcommingMessage} res The response30 * @param {Function} next Go to the next middleware31 */3233exports.one = async function one(req, res, next) {34 res.json({ message: "got one task" });35};3637/**38 * Update todo39 * @controller Update40 * @param {Express.Request} req The request41 * @param {OutcommingMessage} res The response42 * @param {Function} next Go to the next middleware43 */4445exports.update = async function update(req, res, next) {46 res.json({ message: "Updated one task" });47};4849/**50 * Remove todo51 * @controller Remove52 * @param {Express.Request} req The request53 * @param {OutcommingMessage} res The response54 * @param {Function} next Go to the next middleware55 */5657exports.remove = async function remove(req, res, next) {58 res.json({ message: "deleted one task" });59};
now let's get back to our modules/todo/iam/main.server.iam.js
file and require in our controllers on the top
modules/todo/iam/main.server.iam.js
2const ctrls = require("../controllers/main.server.controller");45/**6 * @type { IAM.default }7 */8module.exports = {9 prefix: "/todo",10 routes: [11 {12 path: "/",13 methods: {14 get: {15 iam: "modules:todo:main:list",16 title: "List todo",17 groups: [],18 parents: ["modules:todo", "modules:todo:main"],19 description: "List available todo",21 middlewares: [ctrls.list],23 },2425 post: {26 iam: "modules:todo:main:create",27 title: "Create new task",28 groups: [],29 parents: ["modules:todo", "modules:todo:main"],30 description: "Create new task",32 middlewares: [ctrls.create],34 },35 },36 },37 {38 path: "/:taskId",39 methods: {40 get: {41 iam: "modules:todo:main:one:get",42 title: "Get task",43 groups: [],44 parents: ["modules:todo", "modules:todo:main"],45 description: "Get specific task",47 middlewares: [ctrls.one],49 },50 put: {51 iam: "modules:todo:main:one:update",52 title: "Update task",53 groups: [],54 parents: ["modules:todo", "modules:todo:main"],55 description: "Modify task",57 middlewares: [ctrls.update],59 },60 delete: {61 iam: "modules:todo:main:one:remove",62 title: "Remove task",63 groups: [],64 parents: ["modules:todo", "modules:todo:main"],65 description: "Remove an existing task",67 middlewares: [ctrls.remove],69 },70 },71 },72 ],73};
And now our next step is to create a model for our todo
Edit this page on GitHub