npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

koa-force-https

v2.0.1

Published

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

Downloads

112

Readme

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

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 = 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 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 |

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 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.