0

In this open sourced Node based forum https://github.com/cnodejs/nodeclub/, the application sends a GET request to the root directory to site.index

app.get('/', site.index);

Inside the site.index function below, there is a render function saved to var render, which is obviously responsible for returning the response. Yet, how is this render function called? It is not called anywhere that I can see.

/controllers/site.js

  var User = require('../proxy').User;
  var Topic = require('../proxy').Topic;
  var Tag = require('../proxy').Tag;
  var config = require('../config').config;
  var EventProxy = require('eventproxy');
  exports.index = function (req, res, next) {
  var page = parseInt(req.query.page, 10) || 1;
  var keyword = req.query.q || ''; // in-site search
  if (Array.isArray(keyword)) {
    keyword = keyword.join(' ');
  }
  keyword = keyword.trim();
  var limit = config.list_topic_count;

  var render = function (tags, topics, hot_topics, stars, tops, no_reply_topics, pages) {
    var all_tags = tags.slice(0);

    // 计算最热标签
    tags.sort(function (tag_a, tag_b) {
      return tag_b.topic_count - tag_a.topic_count;
    });

    // 计算最新标签
    tags.sort(function (tag_a, tag_b) {
      return tag_b.create_at - tag_a.create_at;
    });
    var recent_tags = tags.slice(0, 5);
    res.render('index', {
      tags: all_tags,
      topics: topics,
      current_page: page,
      list_topic_count: limit,
      recent_tags: recent_tags,
      hot_topics: hot_topics,
      stars: stars,
      tops: tops,
      no_reply_topics: no_reply_topics,
      pages: pages,
      keyword: keyword
    });
  };

  var proxy = EventProxy.create('tags', 'topics', 'hot_topics', 'stars', 'tops', 'no_reply_topics', 'pages', render);
  proxy.fail(next);
  // 取标签
  Tag.getAllTags(proxy.done('tags'));

  var options = { skip: (page - 1) * limit, limit: limit, sort: [ ['top', 'desc' ], [ 'last_reply_at', 'desc' ] ] };
  var query = {};
  if (keyword) {
    keyword = keyword.replace(/[\*\^\&\(\)\[\]\+\?\\]/g, '');
    query.title = new RegExp(keyword, 'i');
  }
  // 取主题
  Topic.getTopicsByQuery(query, options, proxy.done('topics'));
  // 取热门主题
  Topic.getTopicsByQuery({}, { limit: 5, sort: [ [ 'visit_count', 'desc' ] ] }, proxy.done('hot_topics'));
  // 取星标用户
  User.getUsersByQuery({ is_star: true }, { limit: 5 }, proxy.done('stars'));
  // 取排行榜上的用户
  User.getUsersByQuery({}, { limit: 10, sort: [ [ 'score', 'desc' ] ] }, proxy.done('tops'));
  // 取0回复的主题
  Topic.getTopicsByQuery({ reply_count: 0 }, { limit: 5, sort: [ [ 'create_at', 'desc' ] ] },
  proxy.done('no_reply_topics'));
  // 取分页数据
  Topic.getCountByQuery(query, proxy.done(function (all_topics_count) {
    var pages = Math.ceil(all_topics_count / limit);
    proxy.emit('pages', pages);
  }));
};

1 Answer 1

1

I don't completely know, without looking into the details of eventproxy, but the render function is getting passed in at the end here:

var proxy = EventProxy.create('tags', 'topics', 'hot_topics', 'stars', 'tops', 'no_reply_topics', 'pages', render);

... so it's probably being executed by the EventProxy.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.