mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-13 22:54:42 -04:00
refactor(app.js): Extract middleware to module
extract check URi is valid, redirect without trailing slashes
This commit is contained in:
parent
7ba0d600f1
commit
dee77c459a
3 changed files with 33 additions and 20 deletions
14
lib/web/middleware/checkURiValid.js
Normal file
14
lib/web/middleware/checkURiValid.js
Normal file
|
@ -0,0 +1,14 @@
|
|||
'use strict'
|
||||
|
||||
const logger = require('../../logger')
|
||||
const response = require('../../response')
|
||||
|
||||
module.exports = function (req, res, next) {
|
||||
try {
|
||||
decodeURIComponent(req.path)
|
||||
} catch (err) {
|
||||
logger.error(err)
|
||||
return response.errorBadRequest(res)
|
||||
}
|
||||
next()
|
||||
}
|
17
lib/web/middleware/redirectWithoutTrailingSlashes.js
Normal file
17
lib/web/middleware/redirectWithoutTrailingSlashes.js
Normal file
|
@ -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()
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue