I am trying to build a nodejs app with mysql and what I want is that my controllers and messages that I send should be in separate file as shown below
this is my auth controller file
exports.signup = (req, res) => {
try {
const { name, username, email, phone_number, gender } = req.body;
const payload = [name, username, email, phone_number, gender];
connection.query(signupUserQuery, payload, (error) => {
if (error) {
return errorMessage('Signup Failed');
} else {
return createdMessage('Signup SuccessFul');
}
});
} catch (error) {
return errorMessage('Signup Failed');
}
};
and this is my messages file where I keep my messages
module.exports = {
createdMessage: function (message) {
return res.status(201).json({
isError: false,
message: message,
});
},
errorMessage: function (message) {
return res.status(500).json({
isError: true,
message: message,
});
},
};
So what I want is I want to return this function whenever controller is executed but the problem is when I try to do this I got error as res is not defined , So is there any way to use res in this messages.js File
What I have tried is I send res object from controller and that works but that is repetitive and I do not want to repeat myself
And one more thing I write my queries in separate file link this
insert into tbl_user (name,username,email,phone_number,gender) values (?,?,?,?,?)
but here the problem is I have to put question marks according to fields I require so is there any way to do that in single question mark ?