mirror of
https://github.com/cnodejs/nodeclub.git
synced 2025-12-08 19:55:55 +00:00
给 Response 扩展 render404, renderError 函数,替代之前重复 render 错误页面的代码;
This commit is contained in:
parent
bb72c5ffde
commit
53b2a37b47
2
app.js
2
app.js
@ -23,6 +23,7 @@ var githubStrategyMiddleware = require('./middlewares/github_strategy');
|
||||
var webRouter = require('./web_router');
|
||||
var apiRouterV1 = require('./api_router_v1');
|
||||
var auth = require('./middlewares/auth');
|
||||
var errorPageMiddleware = require("./middlewares/error_page");
|
||||
var proxyMiddleware = require('./middlewares/proxy');
|
||||
var RedisStore = require('connect-redis')(session);
|
||||
var _ = require('lodash');
|
||||
@ -91,6 +92,7 @@ app.use(passport.initialize());
|
||||
|
||||
// custom middleware
|
||||
app.use(auth.authUser);
|
||||
app.use(errorPageMiddleware.errorPage);
|
||||
app.use(auth.blockUser());
|
||||
|
||||
|
||||
|
||||
@ -21,9 +21,7 @@ exports.add = function (req, res, next) {
|
||||
|
||||
var str = validator.trim(content);
|
||||
if (str === '') {
|
||||
res.status(422);
|
||||
res.render('notify/notify', {error: '回复内容不能为空!'});
|
||||
return;
|
||||
return res.renderError('回复内容不能为空!', 422);
|
||||
}
|
||||
|
||||
var ep = EventProxy.create();
|
||||
@ -35,6 +33,7 @@ exports.add = function (req, res, next) {
|
||||
// just 404 page
|
||||
return next();
|
||||
}
|
||||
|
||||
if (topic.lock) {
|
||||
return res.status(403).send('此主题已锁定。');
|
||||
}
|
||||
@ -117,9 +116,7 @@ exports.showEdit = function (req, res, next) {
|
||||
|
||||
Reply.getReplyById(reply_id, function (err, reply) {
|
||||
if (!reply) {
|
||||
res.status(422);
|
||||
res.render('notify/notify', {error: '此回复不存在或已被删除。'});
|
||||
return;
|
||||
return res.render404('此回复不存在或已被删除。');
|
||||
}
|
||||
if (req.session.user._id.equals(reply.author_id) || req.session.user.is_admin) {
|
||||
res.render('reply/edit', {
|
||||
@ -127,8 +124,7 @@ exports.showEdit = function (req, res, next) {
|
||||
content: reply.content
|
||||
});
|
||||
} else {
|
||||
res.status(403);
|
||||
res.render('notify/notify', {error: '对不起,你不能编辑此回复。'});
|
||||
return res.renderError('对不起,你不能编辑此回复。', 403);
|
||||
}
|
||||
});
|
||||
};
|
||||
@ -141,8 +137,7 @@ exports.update = function (req, res, next) {
|
||||
|
||||
Reply.getReplyById(reply_id, function (err, reply) {
|
||||
if (!reply) {
|
||||
res.render('notify/notify', {error: '此回复不存在或已被删除。'});
|
||||
return;
|
||||
return res.render404('此回复不存在或已被删除。');
|
||||
}
|
||||
|
||||
if (String(reply.author_id) === req.session.user._id.toString() || req.session.user.is_admin) {
|
||||
@ -156,10 +151,10 @@ exports.update = function (req, res, next) {
|
||||
res.redirect('/topic/' + reply.topic_id + '#' + reply._id);
|
||||
});
|
||||
} else {
|
||||
res.render('notify/notify', {error: '回复的字数太少。'});
|
||||
return res.renderError('回复的字数太少。', 400);
|
||||
}
|
||||
} else {
|
||||
res.render('notify/notify', {error: '对不起,你不能编辑此回复。'});
|
||||
return res.renderError('对不起,你不能编辑此回复。', 403);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@ -36,9 +36,7 @@ exports.index = function (req, res, next) {
|
||||
|
||||
var topic_id = req.params.tid;
|
||||
if (topic_id.length !== 24) {
|
||||
return res.render('notify/notify', {
|
||||
error: '此话题不存在或已被删除。'
|
||||
});
|
||||
return res.render404('此话题不存在或已被删除。');
|
||||
}
|
||||
var events = ['topic', 'other_topics', 'no_reply_topics'];
|
||||
var ep = EventProxy.create(events, function (topic, other_topics, no_reply_topics) {
|
||||
@ -55,7 +53,7 @@ exports.index = function (req, res, next) {
|
||||
Topic.getFullTopic(topic_id, ep.done(function (message, topic, author, replies) {
|
||||
if (message) {
|
||||
ep.unbind();
|
||||
return res.render('notify/notify', { error: message });
|
||||
return res.renderError(message);
|
||||
}
|
||||
|
||||
topic.visit_count += 1;
|
||||
@ -176,7 +174,7 @@ exports.showEdit = function (req, res, next) {
|
||||
|
||||
Topic.getTopicById(topic_id, function (err, topic, tags) {
|
||||
if (!topic) {
|
||||
res.render('notify/notify', {error: '此话题不存在或已被删除。'});
|
||||
res.render404('此话题不存在或已被删除。');
|
||||
return;
|
||||
}
|
||||
|
||||
@ -190,7 +188,7 @@ exports.showEdit = function (req, res, next) {
|
||||
tabs: config.tabs
|
||||
});
|
||||
} else {
|
||||
res.render('notify/notify', {error: '对不起,你不能编辑此话题。'});
|
||||
res.renderError('对不起,你不能编辑此话题。', 403);
|
||||
}
|
||||
});
|
||||
};
|
||||
@ -203,7 +201,7 @@ exports.update = function (req, res, next) {
|
||||
|
||||
Topic.getTopicById(topic_id, function (err, topic, tags) {
|
||||
if (!topic) {
|
||||
res.render('notify/notify', {error: '此话题不存在或已被删除。'});
|
||||
res.render404('此话题不存在或已被删除。');
|
||||
return;
|
||||
}
|
||||
|
||||
@ -251,7 +249,7 @@ exports.update = function (req, res, next) {
|
||||
|
||||
});
|
||||
} else {
|
||||
res.render('notify/notify', {error: '对不起,你不能编辑此话题。'});
|
||||
res.renderError('对不起,你不能编辑此话题。', 403);
|
||||
}
|
||||
});
|
||||
};
|
||||
@ -290,7 +288,7 @@ exports.top = function (req, res, next) {
|
||||
var topic_id = req.params.tid;
|
||||
var referer = req.get('referer');
|
||||
if (topic_id.length !== 24) {
|
||||
res.render('notify/notify', {error: '此话题不存在或已被删除。'});
|
||||
res.render404('此话题不存在或已被删除。');
|
||||
return;
|
||||
}
|
||||
Topic.getTopic(topic_id, function (err, topic) {
|
||||
@ -298,7 +296,7 @@ exports.top = function (req, res, next) {
|
||||
return next(err);
|
||||
}
|
||||
if (!topic) {
|
||||
res.render('notify/notify', {error: '此话题不存在或已被删除。'});
|
||||
res.render404('此话题不存在或已被删除。');
|
||||
return;
|
||||
}
|
||||
topic.top = !topic.top;
|
||||
@ -321,7 +319,7 @@ exports.good = function (req, res, next) {
|
||||
return next(err);
|
||||
}
|
||||
if (!topic) {
|
||||
res.render('notify/notify', {error: '此话题不存在或已被删除。'});
|
||||
res.render404('此话题不存在或已被删除。');
|
||||
return;
|
||||
}
|
||||
topic.good = !topic.good;
|
||||
@ -344,7 +342,7 @@ exports.lock = function (req, res, next) {
|
||||
return next(err);
|
||||
}
|
||||
if (!topic) {
|
||||
res.render('notify/notify', {error: '此话题不存在或已被删除。'});
|
||||
res.render404('此话题不存在或已被删除。');
|
||||
return;
|
||||
}
|
||||
topic.lock = !topic.lock;
|
||||
|
||||
@ -22,7 +22,7 @@ exports.index = function (req, res, next) {
|
||||
return next(err);
|
||||
}
|
||||
if (!user) {
|
||||
res.render('notify/notify', {error: '这个用户不存在。'});
|
||||
res.render404('这个用户不存在。');
|
||||
return;
|
||||
}
|
||||
|
||||
@ -254,7 +254,7 @@ exports.list_topics = function (req, res, next) {
|
||||
|
||||
User.getUserByLoginName(user_name, function (err, user) {
|
||||
if (!user) {
|
||||
res.render('notify/notify', {error: '这个用户不存在。'});
|
||||
res.render404('这个用户不存在。');
|
||||
return;
|
||||
}
|
||||
|
||||
@ -289,7 +289,7 @@ exports.list_replies = function (req, res, next) {
|
||||
|
||||
User.getUserByLoginName(user_name, function (err, user) {
|
||||
if (!user) {
|
||||
res.render('notify/notify', {error: '这个用户不存在。'});
|
||||
res.render404('这个用户不存在。');
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
17
middlewares/error_page.js
Normal file
17
middlewares/error_page.js
Normal file
@ -0,0 +1,17 @@
|
||||
/**
|
||||
* 需要登录
|
||||
*/
|
||||
exports.errorPage = function (req, res, next) {
|
||||
res.render404 = function(error) {
|
||||
return res.status(404).render('notify/notify', { error: error });
|
||||
}
|
||||
|
||||
res.renderError = function(error, statusCode) {
|
||||
if (statusCode === undefined) {
|
||||
statusCode = 400;
|
||||
}
|
||||
return res.status(statusCode).render('notify/notify', { error: error });
|
||||
}
|
||||
|
||||
next();
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user