I have code like this, very simple.
var common = require("./common");
app.get("/", function(req, res, next) {
doSomething(req, res);
if (err) {
sendError(res, err);
} else {
sendResponse(res, values)
}
});
And these functions are common. They are stored in a separate file.
exports.sendResponse = function(res, values) {
// do something
res.json(values);
};
exports.sendError = function(res, err) {
// do something
res.json(err);
};
exports.doSomething = function(req, res) {
// do something, need to access the req and res variables
};
I have a standard output format. So I pass the results to the sendResponse and sendError functions for better handling. It works but I feel a bit silly. The question is, every time I need to pass the res and req parameter to the common functions. Is there a clever way to avoid passing res and req every time?
Aucun commentaire:
Enregistrer un commentaire