v1.18.x
barisusakli 11 years ago
parent bf18e70d66
commit 3bd4d0e418

@ -62,7 +62,8 @@ middleware.addSlug = function(req, res, next) {
if (err || !slug || slug === id + '/') { if (err || !slug || slug === id + '/') {
return next(err); return next(err);
} }
res.redirect(name + encodeURI(slug)); var url = name + encodeURI(slug);
res.locals.isAPI ? res.json(302, url) : res.redirect(url);
}); });
} }
@ -86,10 +87,13 @@ middleware.checkPostIndex = function(req, res, next) {
} }
var postIndex = parseInt(req.params.post_index, 10); var postIndex = parseInt(req.params.post_index, 10);
postCount = parseInt(postCount, 10) + 1; postCount = parseInt(postCount, 10) + 1;
var url = '';
if (postIndex > postCount) { if (postIndex > postCount) {
return res.locals.isAPI ? res.json(302, '/topic/' + req.params.topic_id + '/' + req.params.slug + '/' + postCount) : res.redirect('/topic/' + req.params.topic_id + '/' + req.params.slug + '/' + postCount); url = '/topic/' + req.params.topic_id + '/' + req.params.slug + '/' + postCount;
return res.locals.isAPI ? res.json(302, url) : res.redirect(url);
} else if (postIndex < 1) { } else if (postIndex < 1) {
return res.locals.isAPI ? res.json(302, '/topic/' + req.params.topic_id + '/' + req.params.slug) : res.redirect('/topic/' + req.params.topic_id + '/' + req.params.slug); url = '/topic/' + req.params.topic_id + '/' + req.params.slug;
return res.locals.isAPI ? res.json(302, url) : res.redirect(url);
} }
next(); next();
}); });

@ -55,7 +55,7 @@ function topicRoutes(app, middleware, controllers) {
app.get('/api/topic/:topic_id/:slug/:post_index?', middleware.checkPostIndex, controllers.topics.get); app.get('/api/topic/:topic_id/:slug/:post_index?', middleware.checkPostIndex, controllers.topics.get);
app.get('/topic/:topic_id/:slug?', middleware.buildHeader, middleware.addSlug, controllers.topics.get); app.get('/topic/:topic_id/:slug?', middleware.buildHeader, middleware.addSlug, controllers.topics.get);
app.get('/api/topic/:topic_id/:slug?', controllers.topics.get); app.get('/api/topic/:topic_id/:slug?', middleware.addSlug, controllers.topics.get);
} }
function tagRoutes(app, middleware, controllers) { function tagRoutes(app, middleware, controllers) {

Loading…
Cancel
Save