Created
November 30, 2016 13:42
-
-
Save fannarsh/4d9ff95507c4a7fdb2038a7e0e6ac13e to your computer and use it in GitHub Desktop.
small thingie that redirects http to https
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
'use strict' | |
var http = require('http') | |
module.exports = function (options) { | |
var log = options.log | |
var ports = options.ports | |
var server = http.createServer() | |
server.on('request', function (req, res) { | |
var host = req.headers.host | |
if (host) { | |
res.setHeader( | |
'Location', | |
'https://' + host.replace(/:\d+/, ':' + ports.https) + req.url | |
) | |
res.statusCode = 302 | |
} else { | |
// connection on ip address instead of host/dns name | |
if (req.httpVersion === '1.0') { | |
res.statusCode = 403 | |
} else { | |
res.statusCode = 400 | |
} | |
} | |
res.end() | |
}) | |
server.listen(ports.http, function () { | |
log.info('Redirecting all http (%s) traffic to https (%s)', ports.http, ports.https) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment