From 645e9a2efb7cb18aa86e01a1dc0f4891db4df740 Mon Sep 17 00:00:00 2001 From: Kevin Haight Date: Mon, 21 May 2018 21:35:51 -0400 Subject: [PATCH] added wildcard support --- package.json | 3 ++- web.js | 10 ++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 334cb8a..1b76cb3 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "Super simple Node app that redirects all requests for an old Heroku app to a new URL", "main": "web.js", "dependencies": { - "express": "^4.15.3" + "express": "^4.15.3", + "express-subdomain": "^1.0.5" }, "devDependencies": {}, "scripts": { diff --git a/web.js b/web.js index b0cc3dd..744271a 100644 --- a/web.js +++ b/web.js @@ -1,14 +1,16 @@ var express = require('express'); +var subdomain = require('express-subdomain'); var app = express(); -var newBaseURL = process.env.NEW_BASE_URL || 'http://example.com'; +var newBaseURL = process.env.NEW_BASE_URL || 'http://www.example.com'; var redirectStatus = parseInt(process.env.REDIRECT_STATUS || 302); var port = process.env.PORT || 5000; app.get('*', function(request, response) { - response.redirect(redirectStatus, newBaseURL + request.url); + var baseURL = newBaseURL.replace('*', request.subdomains[0]); + response.redirect(redirectStatus, baseURL + request.url); }); app.listen(port, function() { - console.log("Listening on " + port); -}); \ No newline at end of file + console.log('Listening on ' + port); +});