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
在这个基于开源节点的论坛https://github.com/cnodejs/nodeclub/中,应用程序向根目录发送GET请求到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.
在下面的site.index函数中,有一个渲染函数保存到var render,这显然负责返回响应。然而,这个渲染函数是如何调用的?我无法在任何地方看到它。
/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 个解决方案
#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:
我不完全知道,没有查看eventproxy的细节,但渲染函数最终在这里传入:
var proxy = EventProxy.create('tags', 'topics', 'hot_topics', 'stars', 'tops', 'no_reply_topics', 'pages', render);
... so it's probably being executed by the EventProxy.
...所以它可能是由EventProxy执行的。
#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:
我不完全知道,没有查看eventproxy的细节,但渲染函数最终在这里传入:
var proxy = EventProxy.create('tags', 'topics', 'hot_topics', 'stars', 'tops', 'no_reply_topics', 'pages', render);
... so it's probably being executed by the EventProxy.
...所以它可能是由EventProxy执行的。