hedgedoc/lib/web/middleware/redirectWithoutTrailingSlashes.ts
Yannick Bungers 6cbd436454
Refactored middleware -> Typescript
Signed-off-by: Yannick Bungers <git@innay.de>
Signed-off-by: David Mehren <dmehren1@gmail.com>
2020-04-25 16:04:11 +02:00

16 lines
605 B
TypeScript

import { NextFunction, Request, Response } from 'express'
import { config } from '../../config'
export default function (req: Request, res: Response, next: NextFunction): void {
if (req.method === 'GET' && req.path.substr(-1) === '/' && req.path.length > 1) {
const queryString: string = req.url.slice(req.path.length)
const urlPath: string = req.path.slice(0, -1)
let serverURL: string = config.serverURL
if (config.urlPath) {
serverURL = serverURL.slice(0, -(config.urlPath.length + 1))
}
res.redirect(301, serverURL + urlPath + queryString)
} else {
next()
}
}