mirror of
https://github.com/Feuerhamster/mailform.git
synced 2025-05-04 12:46:18 +02:00
move redirect to util to keep express-routes isolated
This commit is contained in:
parent
c9af890f08
commit
263729b2fd
2 changed files with 21 additions and 11 deletions
|
@ -7,6 +7,7 @@ import validate from "./services/validate";
|
||||||
import {postBody} from "./models/post";
|
import {postBody} from "./models/post";
|
||||||
import {EmailService} from "./services/email";
|
import {EmailService} from "./services/email";
|
||||||
import {CaptchaService} from "./services/captcha";
|
import {CaptchaService} from "./services/captcha";
|
||||||
|
import getRedirectUrl from "./util/redirect";
|
||||||
|
|
||||||
const router: Router = Router();
|
const router: Router = Router();
|
||||||
|
|
||||||
|
@ -111,17 +112,6 @@ router.post("/:target", async (req: Request, res: Response) => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
function getRedirectUrl(req: Request, targetRedirectUrl: string) {
|
|
||||||
let redirectUrl = targetRedirectUrl
|
|
||||||
|
|
||||||
const urlPattern = /^(https?):\/\//;
|
|
||||||
if(!urlPattern.test(redirectUrl)) {
|
|
||||||
redirectUrl = (req.header('Referer') || '/') + redirectUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
return redirectUrl
|
|
||||||
}
|
|
||||||
|
|
||||||
router.all("*", (req: Request, res: Response) => res.status(404).end());
|
router.all("*", (req: Request, res: Response) => res.status(404).end());
|
||||||
|
|
||||||
export default router;
|
export default router;
|
20
src/util/redirect.ts
Executable file
20
src/util/redirect.ts
Executable file
|
@ -0,0 +1,20 @@
|
||||||
|
import {Request} from "express";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create absolute Url from relative redirectUrl
|
||||||
|
* @param req incoming Request
|
||||||
|
* @param targetRedirectUrl redirectUrl from target
|
||||||
|
* @returns String
|
||||||
|
*/
|
||||||
|
function getRedirectUrl(req: Request, targetRedirectUrl: string) {
|
||||||
|
let redirectUrl = targetRedirectUrl
|
||||||
|
|
||||||
|
const urlPattern = /^(https?):\/\//;
|
||||||
|
if(!urlPattern.test(redirectUrl)) {
|
||||||
|
redirectUrl = (req.header('Referer') || '/') + redirectUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return redirectUrl
|
||||||
|
}
|
||||||
|
|
||||||
|
export default getRedirectUrl;
|
Loading…
Add table
Reference in a new issue