Skip to content

Instantly share code, notes, and snippets.

@fannarsh
Created November 30, 2016 13:42
Show Gist options
  • Save fannarsh/4d9ff95507c4a7fdb2038a7e0e6ac13e to your computer and use it in GitHub Desktop.
Save fannarsh/4d9ff95507c4a7fdb2038a7e0e6ac13e to your computer and use it in GitHub Desktop.
small thingie that redirects http to https
'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