HTTPS redirect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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