force-ssl
Express middleware for forcing ssl. Redirects http requests to https. Tested on Express 4.x
Install
npm install force-ssl
Usage
Prerequisites for this example:
npm install express self-signed-https force-ssl
Javascript code (paste into app.js):
var express = var selfSignedHttps = var forceSsl = var app = app // http on port 80 // https on port 443app app
You may need to run it as root (since it listens on port 80):
sudo node app.js
Now if you visit http://localhost, it should redirect you to https://localhost
Options
If HTTPS is running on a different port, then set the https_port option to specify it:
var express = var selfSignedHttps = var forceSsl = var app = app // http on port 3000 // https on port 3001forceSslhttps_port = 3001 // specify that https is running on port 3001app app
Now if you visit http://localhost:3000, it should redirect you to https://localhost:3001
License
MIT