Skip to content

Instantly share code, notes, and snippets.

@dhobbs
Last active December 20, 2015 16:49
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save dhobbs/6164710 to your computer and use it in GitHub Desktop.
HTTPS redirect
#!/usr/bin/env node
'use strict';
var http = require('http');
var listenPort = process.argv[2];
if (listenPort == undefined || isNaN(listenPort)) throw new Error('No valid port number specified. Usage: httpsRedirect [portToListenOn]');
console.log('HTTPS Redirector listening on port ' + listenPort);
// Redirect everything to specified port
function newLocation(req) {
var redirectTo = 'https://' + req.headers.host.replace(/:.*/,'') + req.url;
console.log('Redirect to: ' + redirectTo);
return redirectTo;
}
var srv = http.createServer(function (req, res) {
res.writeHead(301, {'Content-Type': '*/*; charset="UTF-8"', 'Location': newLocation(req), 'Content-Length': 0 });
res.end();
});
srv.listen(listenPort);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment