From 3bd4d0e418d8433f9087141ebaa46f17374ad3bb Mon Sep 17 00:00:00 2001 From: barisusakli Date: Sat, 14 Jun 2014 14:12:33 -0400 Subject: [PATCH] closes #1692 --- src/middleware/middleware.js | 10 +++++++--- src/routes/index.js | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/middleware/middleware.js b/src/middleware/middleware.js index bdcc74b69f..371cf21744 100644 --- a/src/middleware/middleware.js +++ b/src/middleware/middleware.js @@ -62,7 +62,8 @@ middleware.addSlug = function(req, res, next) { if (err || !slug || slug === id + '/') { 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); postCount = parseInt(postCount, 10) + 1; + var url = ''; 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) { - 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(); }); diff --git a/src/routes/index.js b/src/routes/index.js index 737d3e2a39..eaab5ff63c 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -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('/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) {