From 6837d76fb6d17c08c6540a12bbfd5dcd5c5b0ceb Mon Sep 17 00:00:00 2001 From: Billy Moon Date: Mon, 11 Apr 2016 01:23:02 +0100 Subject: [PATCH] Handle absolute paths without domain More details here: http://stackoverflow.com/questions/36534971/node-express-proxy-middleware-handle-redirect --- index.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index b8c573e..8eacae1 100644 --- a/index.js +++ b/index.js @@ -58,9 +58,14 @@ module.exports = function proxyMiddleware(options) { , headers = myRes.headers , location = headers.location; // Fix the location - if (((statusCode > 300 && statusCode < 304) || statusCode === 201) && location && location.indexOf(options.href) > -1) { - // absoulte path - headers.location = location.replace(options.href, slashJoin('/', slashJoin((options.route || ''), ''))); + if (((statusCode > 300 && statusCode < 304) || statusCode === 201) && location) { + // absolute path with domain + if(location.indexOf(options.href) > -1) { + headers.location = location.replace(options.href, slashJoin('/', slashJoin((options.route || ''), ''))); + // absolute path without domain + } else if(location[0] === '/' && req.originalUrl.slice(req.url.length*-1) === req.url) { + headers.location = req.originalUrl.slice(0, req.url.length*-1) + headers.location; + } } applyViaHeader(myRes.headers, opts, myRes.headers); rewriteCookieHosts(myRes.headers, opts, myRes.headers, req);