unjs / redirect-ssl

Connect/Express middleware to enforce https using is-https
MIT License
100 stars 15 forks source link
connect express force-https force-ssl https middleware pwa

redirect-ssl

Connect/Express middleware to enforce https using is-https.

version downloads ci

Usage

Install package:

yarn add redirect-ssl
# or
npm install redirect-ssl

Require and use redirect-ssl. Make sure to use this middlware as the first in your middleware chain (if using express see middleware chain:

import redirectSSL from 'redirect-ssl'
// or
const redirectSSL = require('redirect-ssl')

// Add middleware
app.use(redirectSSL)

// Using custom options
app.use(redirectSSL.create({ redirectPort: 8443 }))

Disable for non-production or localhost

If you want to disable on localhost, use the exclude option:

app.use(redirectSSL.create({
   exclude: ['localhost']
}))

Only enable in production environments:

app.use(redirectSSL.create({
  enabled: process.env.NODE_ENV === 'production'
}))

Options

trustProxy

Trust and check x-forwarded-proto header for HTTPS detection.

enabled

redirectPort

Redirect users to this port for HTTPS. (:443 is omitted from URL as is default for https:// schema)

redirectHost

Redirects using this value as host, if omitted will use request host for redirects.

NOTE It should not contain schema or trailing slashes. (Example: google.com)

redirectUnknown

Redirect when no SSL detection method is available too. disable this option if you encounter redirect loops.

statusCode

Status code when redirecting. The reason of choosing 307 for default is:

exclude

An array of routes patterns for which redirection should be disabled.

Using with Nuxt.js

Add the redirect-ssl to the serverMiddleware array within in the nuxt.config.js file is the preferred usage:

import redirectSSL from 'redirect-ssl'

export default {
  serverMiddleware: [
    redirectSSL.create({
      enabled: process.env.NODE_ENV === 'production'
     }),
  ]
}

You will still need to install this package within your project for it work.

License

MIT. Made with 💖