Koa.js middleware to force HTTPS connection on any incoming requests. In case of a non-encrypted HTTP request, koa-force-https
automatically redirects to an HTTPS address.
Requirements
- Koa@2+
- Node@6.13+
Installation
npm install koa-force-https --save
Options
Name | Type | Default | Description |
---|---|---|---|
port |
Integer | HTTPS port (port :443 is automatically removed from the URL) |
|
hostname |
String | same host |
Hostname for redirect |
httpStatusCode |
Integer | 301 |
HTTP status code for redirect |
Usage
const Koa = ;const forceHTTPS = ; const app = ; app;
Examples
http
to https
Redirect requests from const fs = ;const http = ;const https = ;const Koa = ;const forceHTTPS = ; const options = key: fs cert: fs const app = ; app; app; // Runs 2 servers for the application// Requests from the HTTP server will be redirected to the HTTPS serverhttp;https;
Some results requests for this example
Request URL | Status Code | Location |
---|---|---|
http://example.com |
301 |
https://example.com/ |
http://www.example.com |
301 |
https://www.example.com/ |
http://www.example.com/news |
301 |
https://www.example.com/news |
http://www.example.com/?id=1 |
301 |
https://www.example.com/?id=1 |
http://example.com/news?id=1 |
301 |
https://example.com/news?id=1 |
https://example.com |
200 |
no redirect |
https://www.example.com |
200 |
no redirect |
http
to https
(using HTTP/2 protocol) to hostname example.com
using the HTTP status code 307
("307 Temporary Redirect")
Redirect requests from const fs = ;const http = ;const http2 = ;const Koa = ;const forceHTTPS = ; const options = key: fs cert: fs const app = ; app; app; http;http2;
Results requests for this example
Request URL | Status Code | Location |
---|---|---|
http://example.com |
307 |
https://example.com/ |
http://www.example.com |
307 |
https://example.com/ |
http://www.example.com/news |
307 |
https://example.com/news |
http://www.example.com/?id=1 |
307 |
https://example.com/?id=1 |
http://example.com/news?id=1 |
307 |
https://example.com/news?id=1 |
https://example.com |
200 |
no redirect |
https://www.example.com |
200 |
no redirect |
License
koa-force-https
is MIT licensed.