Skip to content

Commit

Permalink
Added relative URL support for redirect() method.
Browse files Browse the repository at this point in the history
  • Loading branch information
coreybutler committed Aug 9, 2020
1 parent 4dcffe7 commit c02369b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
14 changes: 14 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,20 @@ class Endpoint {
redirect (url, permanent = false, moved = false) {
return (req, res) => {
const code = permanent ? (moved ? 301 : 308) : (moved ? 303 : 307)

// Identify relative redirect
if (!/^\w+:\/{2}/i.test(url)) {
let uri = `${req.protocol}://${req.headers.host || req.host || req.hostname}`

if (/^\.+\//.test(url)) {
uri += req.path + '/' + url
} else {
uri += ('/' + url).replace(/^\/{2,}/, '/')
}

url = uri
}

res.header('location', url)
res.sendStatus(code)
}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@butlerlogic/common-api",
"version": "1.5.0",
"version": "1.5.1",
"description": "An API engineering productivity kit for Express.",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit c02369b

Please sign in to comment.