2.0.1 • Published 5 years ago

koa-force-https v2.0.1

Weekly downloads
41
License
MIT
Repository
github
Last release
5 years ago

koa-force-https · GitHub license npm version

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

NameTypeDefaultDescription
portIntegerHTTPS port (port :443 is automatically removed from the URL)
hostnameStringsame hostHostname for redirect
httpStatusCodeInteger301HTTP status code for redirect

Usage

const Koa = require('koa');
const forceHTTPS = require('koa-force-https');

const app = new Koa();

app.use(forceHTTPS());

Examples

Redirect requests from http to https

const fs = require('fs');
const http = require('http');
const https = require('https');
const Koa = require('koa');
const forceHTTPS = require('koa-force-https');

const options = {
  key: fs.readFileSync('ssl-certificate.key'),
  cert: fs.readFileSync('ssl-certificate.crt')
}

const app = new Koa();

app.use(forceHTTPS());

app.use((ctx) => {
  ctx.body = 'Hello! This is an HTTPS connection.';
});

// Runs 2 servers for the application
// Requests from the HTTP server will be redirected to the HTTPS server
http.createServer(app.callback()).listen(80);
https.createServer(options, app.callback()).listen(443);

Some results requests for this example

Request URLStatus CodeLocation
http://example.com301https://example.com/
http://www.example.com301https://www.example.com/
http://www.example.com/news301https://www.example.com/news
http://www.example.com/?id=1301https://www.example.com/?id=1
http://example.com/news?id=1301https://example.com/news?id=1
https://example.com200no redirect
https://www.example.com200no redirect

Redirect requests from http to https (using HTTP/2 protocol) to hostname example.com using the HTTP status code 307 ("307 Temporary Redirect")

const fs = require('fs');
const http = require('http');
const http2 = require('http2');
const Koa = require('koa');
const forceHTTPS = require('koa-force-https');

const options = {
  key: fs.readFileSync('ssl-certificate.key'),
  cert: fs.readFileSync('ssl-certificate.crt')
}

const app = new Koa();

app.use(forceHTTPS(undefined, 'example.com', 307));

app.use((ctx) => {
  ctx.body = 'Hello! This is an HTTPS connection using HTTP/2 protocol.';
});

http.createServer(app.callback()).listen(80);
http2.createSecureServer(options, app.callback()).listen(443);

Results requests for this example

Request URLStatus CodeLocation
http://example.com307https://example.com/
http://www.example.com307https://example.com/
http://www.example.com/news307https://example.com/news
http://www.example.com/?id=1307https://example.com/?id=1
http://example.com/news?id=1307https://example.com/news?id=1
https://example.com200no redirect
https://www.example.com200no redirect

License

koa-force-https is MIT licensed.