Permalink
Browse files

add showJson option

  • Loading branch information...
1 parent b6e1a32 commit 88f5d9a141eb6b6dbb785ebbd8b79df3b75555eb @alsotang alsotang committed Oct 23, 2016
Showing with 11 additions and 8 deletions.
  1. +2 −2 api_router_v1.js
  2. +5 −2 middlewares/limit.js
  3. +1 −1 test/middlewares/limit.test.js
  4. +3 −3 web_router.js
View
@@ -15,7 +15,7 @@ var router = express.Router();
// 主题
router.get('/topics', topicController.index);
router.get('/topic/:id', middleware.tryAuth, topicController.show);
-router.post('/topics', middleware.auth, limit.peruserperday('create_topic', config.create_post_per_day, true), topicController.create);
+router.post('/topics', middleware.auth, limit.peruserperday('create_topic', config.create_post_per_day, {showJson: true}), topicController.create);
router.post('/topics/update', middleware.auth, topicController.update);
@@ -33,7 +33,7 @@ router.get('/user/:loginname', userController.show);
router.post('/accesstoken', middleware.auth, toolsController.accesstoken);
// 评论
-router.post('/topic/:topic_id/replies', middleware.auth, limit.peruserperday('create_reply', config.create_reply_per_day, true), replyController.create);
+router.post('/topic/:topic_id/replies', middleware.auth, limit.peruserperday('create_reply', config.create_reply_per_day, {showJson: true}), replyController.create);
router.post('/reply/:reply_id/ups', middleware.auth, replyController.ups);
// 通知
@@ -5,7 +5,10 @@ var moment = require('moment');
var SEPARATOR = '^_^@T_T';
var makePerDayLimiter = function (identityName, identityFn) {
- return function (name, limitCount, showJson) { // showJson = true 表示调用来自API并返回结构化数据;否则表示调用来自前段并渲染错误页面
+ return function (name, limitCount, options) {
+ /*
+ options.showJson = true 表示调用来自API并返回结构化数据;否则表示调用来自前段并渲染错误页面
+ */
return function (req, res, next) {
var identity = identityFn(req);
var YYYYMMDD = moment().format('YYYYMMDD');
@@ -24,7 +27,7 @@ var makePerDayLimiter = function (identityName, identityFn) {
next();
} else {
res.status(403);
- if (showJson) {
+ if (options.showJson) {
res.send({success: false, error_msg: '频率限制:当前操作每天可以进行 ' + limitCount + ''});
} else {
res.render('notify/notify', { error: '频率限制:当前操作每天可以进行 ' + limitCount + ''});
@@ -12,7 +12,7 @@ describe('test/middlewares/limit.test.js', function () {
before(function () {
app.get('/test_peripperday',
- limitMiddleware.peripperday(visitor, 3, true), function (req, res) {
+ limitMiddleware.peripperday(visitor, 3, {showJson: true}), function (req, res) {
res.send('hello');
});
View
@@ -85,14 +85,14 @@ router.post('/topic/:tid/lock', auth.adminRequired, topic.lock); // 锁定主题
router.post('/topic/:tid/delete', auth.userRequired, topic.delete);
// 保存新建的文章
-router.post('/topic/create', auth.userRequired, limit.peruserperday('create_topic', config.create_post_per_day, false), topic.put);
+router.post('/topic/create', auth.userRequired, limit.peruserperday('create_topic', config.create_post_per_day, {showJson: false}), topic.put);
router.post('/topic/:tid/edit', auth.userRequired, topic.update);
router.post('/topic/collect', auth.userRequired, topic.collect); // 关注某话题
router.post('/topic/de_collect', auth.userRequired, topic.de_collect); // 取消关注某话题
// reply controller
-router.post('/:topic_id/reply', auth.userRequired, limit.peruserperday('create_reply', config.create_reply_per_day, false), reply.add); // 提交一级回复
+router.post('/:topic_id/reply', auth.userRequired, limit.peruserperday('create_reply', config.create_reply_per_day, {showJson: false}), reply.add); // 提交一级回复
router.get('/reply/:reply_id/edit', auth.userRequired, reply.showEdit); // 修改自己的评论页
router.post('/reply/:reply_id/edit', auth.userRequired, reply.update); // 修改某评论
router.post('/reply/:reply_id/delete', auth.userRequired, reply.delete); // 删除某评论
@@ -115,7 +115,7 @@ router.get('/auth/github/callback',
passport.authenticate('github', { failureRedirect: '/signin' }),
github.callback);
router.get('/auth/github/new', github.new);
-router.post('/auth/github/create', limit.peripperday('create_user_per_ip', config.create_user_per_ip, false), github.create);
+router.post('/auth/github/create', limit.peripperday('create_user_per_ip', config.create_user_per_ip, {showJson: false}), github.create);
router.get('/search', search.index);

0 comments on commit 88f5d9a

Please sign in to comment.