Decoupling the logic of the controller with the logic of the model will allow you to reuse the logic and simplify its work.
The idea is that the goal of the controllers is to format input and output to and from your application, while models handle actual data manipulations. (This is a typical Rails-like MVC pattern for the REST API)
For your example:
menuController.js
var menuModel = require('./menuModel'); module.exports.save = function(req, res, next) { menuModel.save(req.body, function(err) { if(err) return next(err); res.json({msg:'menu save'}) }); };
menuModel.js
module.exports.save = function(body, callback) {
userController.js
var userModel = require('./userModel'); module.exports.save = function(req, res, next) { userModel .save(function(err){ if(err) return next(err); res.json({msg:'success'}); }); }
userModel.js
var menuModel = require('./menuModel'); module.exports.save = function(body, callback) {
Rule of thumb, keep as little logic as possible in controllers.
source share