Socket
Book a DemoInstallSign in
Socket

koa-force-https

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-force-https

Koa.js middleware to force HTTPS connection on any incoming requests

2.0.1
latest
Source
npmnpm
Version published
Weekly downloads
6
-14.29%
Maintainers
1
Weekly downloads
 
Created
Source

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.

Keywords

koa

FAQs

Package last updated on 07 Apr 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.