node package manager


Redirect HTTP requests to HTTPS

TLS Server with HTTP Redirect

Put this in front of your app and HTTP requests will be redirected to their TLS equivalent.


var http = require('http'),
    httpls = require('httpls'),
    fs = require('fs');
var options = {
  key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
  cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem')
var app = http.createServer(function(req, res) {
  res.end("hello, secure world\n");
var server = httpls.createServer(options, app);

What happen?

HTTP requests to the listening port will be served a 301 'moved permanently' redirect to the requested location with the scheme changed to https. All other URI attributes are preserved.

curl -I http://localhost:1337/
HTTP/1.1 301 Moved Permanently
Location: https://localhost:1337/
curl https://localhost:1337/
hello, secure world