Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modularize controllers/index.js #35

Closed
rooberrydev opened this issue Dec 20, 2019 · 1 comment
Closed

modularize controllers/index.js #35

rooberrydev opened this issue Dec 20, 2019 · 1 comment
Assignees

Comments

@rooberrydev
Copy link

since you already have an empty file for home, I would move the callback for the "/" route handler to this file.

router.get("/", (req, res) => {
res.render("home", { questions: questionsList });
});

change this to

router.get("/", getHome)

or similar, where getHome would be imported in from home.js

you can do the same for

router.post("/create-user", (req, res) => {
// console.log(req.body);
const questionsArray = [
req.body.q1,
req.body.q2,
req.body.q3,
req.body.q4,
req.body.q5,
req.body.q6,
req.body.q7
];
let goodOrBad;
let totalPoint = behaviour(questionsArray);
if (totalPoint >= 4) {
goodOrBad = true;
} else {
goodOrBad = false;
}
postData({
name: req.body.username,
behaviour: goodOrBad,
category: req.body.category
})
.then(responseDb => {
res.redirect("/presents");
})
.catch(error => {
throw error;
});
// res.redirect('/presents');
});
router.get("/presents", (req, res) => {
console.log("we're getting data");
getData()
.then(users => res.render("presents", { users }))
.catch(err => {
throw err;
});
});

and abstract the callbacks into separate files.

leaving your index.js cleaner and looking like this

router.get("/",getHome)
router.post("/create-user",createUser)
router.get("/presents",getPresents)

where getHome, createUser and getPresents are functions imported in. refer to the express workshop if you need help with this 👍 😄

@rooberrydev
Copy link
Author

#38

by dan is the same issue btw

@AlexandraOM AlexandraOM self-assigned this Dec 20, 2019
AlexandraOM added a commit that referenced this issue Dec 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants