diff --git a/index.js b/index.js index f33e949..e31fde7 100644 --- a/index.js +++ b/index.js @@ -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) } diff --git a/package-lock.json b/package-lock.json index ea3a975..99f9c48 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@butlerlogic/common-api", - "version": "1.5.0", + "version": "1.5.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 513f846..62da504 100644 --- a/package.json +++ b/package.json @@ -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": {