nodeJs,Express得到响应并调用另一个函数

时间:2018-05-18 07:31:01

标签: node.js express

我有post.route.js,我提到了

var post = require('../controllers/post.controller');
router.route('/posts').get(post.getPosts, post.setCache);

我的post.controller.js有

exports.getPosts = function(req, res, next) {
    var qstring = postQueryString;
    getPostsDataByQuery(qstring,req,res,next);
}

function getPostsDataByQuery(queryString,req, res, next){
  logger.info('start',req.route.path);
  // some code here
  return res.json(rows);
  next();
};

exports.setCache = function(req, res, next){
    console.log('here in set function');
   cache.setExp(req, rows);
   return true;
 }

如果在setExp中我记录了不显示我

exports.setExp = function(req, data){
    console.log('here');
  }

1 个答案:

答案 0 :(得分:0)

您可以使用next方法:

function getPosts(queryString, req, res, next){
  // your code
  next();
};
 function setCache(req, res) {
   cache.setExp(req.originalUrl, process.env.CACHE_POST_EXP_TIME, rows);
   return true;
 }

 router.route('/posts').get(post.getPosts, post.setCache);