izmjs

Controllers

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 List
4* @param {Express.Request} req The request
5* @param {OutcommingMessage} res The response
6* @param {Function} next Go to the next middleware
7*/
8
9exports. = async function (req, res, next) {
10
11};

Now let's create all the controllers for our CRUD

modules/todo/controllers/main.server.iam.js
1/**
2 * List todos
3 * @controller List
4 * @param {Express.Request} req The request
5 * @param {OutcommingMessage} res The response
6 * @param {Function} next Go to the next middleware
7 */
8
9exports.list = async function list(req, res, next) {
10 res.json({ message: "hello world" });
11};
12
13/**
14 * Create todo
15 * @controller Create
16 * @param {Express.Request} req The request
17 * @param {OutcommingMessage} res The response
18 * @param {Function} next Go to the next middleware
19 */
20
21exports.create = async function create(req, res, next) {
22 res.status(201).json({ message: "Todo Created" });
23};
24
25/**
26 * List one Todo
27 * @controller One
28 * @param {Express.Request} req The request
29 * @param {OutcommingMessage} res The response
30 * @param {Function} next Go to the next middleware
31 */
32
33exports.one = async function one(req, res, next) {
34 res.json({ message: "got one task" });
35};
36
37/**
38 * Update todo
39 * @controller Update
40 * @param {Express.Request} req The request
41 * @param {OutcommingMessage} res The response
42 * @param {Function} next Go to the next middleware
43 */
44
45exports.update = async function update(req, res, next) {
46 res.json({ message: "Updated one task" });
47};
48
49/**
50 * Remove todo
51 * @controller Remove
52 * @param {Express.Request} req The request
53 * @param {OutcommingMessage} res The response
54 * @param {Function} next Go to the next middleware
55 */
56
57exports.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");
4
5/**
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 },
24
25 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