1.0.1 • Published 10 years ago

https-redirect-server v1.0.1

Weekly downloads
8
License
-
Repository
github
Last release
10 years ago

https-redirect-server

A node module to redirect requests from a non-secure port to a secure port.

what is https-redirect-server?

This is a node module that currently supports two ways of redirecting from a non-secure connection to a secure connection.

If you want to force all traffic to come to your site via a secure connection, a small, lightweight http server can be run to redirect all traffic to https and a secure port.

If you want to force specific routes to use a secure connection, a middleware function can be used with express to redirect insecure traffic to https and a secure port.

Using the redirect server

This will start a server running on an insecure port that will redirect all traffic. Here is an example of how to use it alongside an express server:

var express       = require('express');
var https         = require('https');
var port          = process.env.PORT || 8080;
var securePort    = process.env.PORT || 8443;
var httpsRedirect = require('https-redirect-server');

// configure application and routes
app.get('/', function(req, res, next) {
  // ...
});

app.get('/login', function(req, res, next) {
  // ...
});

// set ssl options
var ssl_options = {
  key: fs.readFileSync(__dirname + '/website.key'),
  cert: fs.readFileSync(__dirname + '/website.cert')
};

// start servers
httpsRedirect(port, securePort).server(); // will redirect anything to receives

https.createServer(ssl_options, app).listen(securePort);

Using the forceSecure middleware

This will only redirect routes that you specify to use the middleware. Here is an example of how to redirect traffic that is going to the login page of an express server:

var express       = require('express');
var http          = require('http');
var https         = require('https');
var port          = process.env.PORT || 8080;
var securePort    = process.env.PORT || 8443;
var httpsRedirect = require('https-redirect-server');

var forceSecure   = httpsRedirect(port, securePort);

// configure application and routes
app.get('/', function(req, res, next) { 
  // ...
});

// add middleware to redirect request if it is not secure
app.get('/login', forceSecure, function(req, res, next) {
  // ...
});

// set ssl options
var ssl_options = {
  key: fs.readFileSync(__dirname + '/website.key'),
  cert: fs.readFileSync(__dirname + '/website.cert')
};

// start servers, one for insecure and one for secure
http.createServer(app).listen(port); 

https.createServer(ssl_options, app).listen(securePort);