summaryrefslogtreecommitdiff
path: root/lib/web/middleware/redirectWithoutTrailingSlashes.js
diff options
context:
space:
mode:
authorMax Wu2017-05-14 17:50:25 +0800
committerGitHub2017-05-14 17:50:25 +0800
commitea9b1557f9834b2b140bd9b07488b5fca3953791 (patch)
tree8e96eb37c1715f860a698d5e7eeb2381b00af9d8 /lib/web/middleware/redirectWithoutTrailingSlashes.js
parentf85d1d880114b6787fdb2bac488341776deaad3d (diff)
parentc37b6669158750ab2222e81a5a8fc3a5c8c353f5 (diff)
Merge pull request #421 from hackmdio/BackendRefactor
Backend code refactoring - part 1
Diffstat (limited to 'lib/web/middleware/redirectWithoutTrailingSlashes.js')
-rw-r--r--lib/web/middleware/redirectWithoutTrailingSlashes.js17
1 files changed, 17 insertions, 0 deletions
diff --git a/lib/web/middleware/redirectWithoutTrailingSlashes.js b/lib/web/middleware/redirectWithoutTrailingSlashes.js
new file mode 100644
index 00000000..fbaba617
--- /dev/null
+++ b/lib/web/middleware/redirectWithoutTrailingSlashes.js
@@ -0,0 +1,17 @@
+'use strict'
+
+const config = require('../../config')
+
+module.exports = function (req, res, next) {
+ if (req.method === 'GET' && req.path.substr(-1) === '/' && req.path.length > 1) {
+ const queryString = req.url.slice(req.path.length)
+ const urlPath = req.path.slice(0, -1)
+ let serverURL = config.serverurl
+ if (config.urlpath) {
+ serverURL = serverURL.slice(0, -(config.urlpath.length + 1))
+ }
+ res.redirect(301, serverURL + urlPath + queryString)
+ } else {
+ next()
+ }
+}