HackMD/lib/web/middleware/redirectWithoutTrailingSlashes.js
BoHong Li dee77c459a refactor(app.js): Extract middleware to module
extract check URi is valid, redirect without trailing slashes
2017-05-08 19:24:37 +08:00

17 lines
501 B
JavaScript

'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()
}
}