HTTPS Redirect Middleware
raw JSON →redirect-ssl is a Connect/Express middleware designed to enforce HTTPS for web applications, ensuring all incoming HTTP requests are redirected to their HTTPS equivalent. It leverages the `is-https` package for robust HTTPS detection, including support for `x-forwarded-proto` headers often found when deployed behind proxies. The current stable version is 3.0.0, which introduced significant changes like a TypeScript rewrite and renaming of key configuration options. While no strict release cadence is stated, major versions appear to introduce breaking changes, signaling active development and modernization. Its key differentiators include flexible configuration for proxy trust, custom redirect ports/hosts, and exclusion patterns, making it adaptable for various deployment scenarios including Nuxt.js applications. It aims to provide a reliable and configurable solution for ensuring secure communication by default.
Common errors
error TypeError: app.use is not a function ↓
redirect-ssl within an Express or Connect application, for example: import express from 'express'; const app = express(); app.use(redirectSSL); error ERR_TOO_MANY_REDIRECTS ↓
redirectUnknown: false in redirectSSL.create() options. If behind a proxy, ensure trustProxy: true is set and the proxy correctly forwards x-forwarded-proto. Warnings
breaking The `xForwardedProto` option was renamed to `trustProxy` in `v2.0.0`. ↓
breaking The `redirect` option was renamed to `enabled` and its default value is always `true` since `v2.0.0`. ↓
breaking Version `2.0.0` and later were rewritten in TypeScript, which might introduce subtle behavior changes for express-like frameworks or specific middleware chains. ↓
gotcha The middleware should be placed as early as possible in your middleware chain to ensure all requests are intercepted for HTTPS redirection. ↓
gotcha Setting `redirectUnknown: true` can cause redirect loops in certain environments where HTTPS detection methods are unavailable or misconfigured, particularly with load balancers or proxies. ↓
Install
npm install redirect-ssl yarn add redirect-ssl pnpm add redirect-ssl Imports
- redirectSSL wrong
const redirectSSL = require('redirect-ssl')correctimport redirectSSL from 'redirect-ssl' - redirectSSL.create wrong
import { create } from 'redirect-ssl'; app.use(create({ redirectPort: 8443 }))correctimport redirectSSL from 'redirect-ssl'; app.use(redirectSSL.create({ redirectPort: 8443 })) - redirectSSL Types
import redirectSSL, { RedirectOptions } from 'redirect-ssl'
Quickstart
import express from 'express';
import redirectSSL from 'redirect-ssl';
const app = express();
const PORT = process.env.PORT || 3000;
const HTTPS_PORT = process.env.HTTPS_PORT || 8443;
// Apply redirect-ssl middleware as the first middleware
// Only enable in production and exclude localhost for development ease
app.use(redirectSSL.create({
enabled: process.env.NODE_ENV === 'production',
exclude: ['localhost'],
redirectPort: HTTPS_PORT
}));
app.get('/', (req, res) => {
res.send('Hello from redirect-ssl example!');
});
// In a real application, you'd also have an HTTPS server listening on HTTPS_PORT
app.listen(PORT, () => {
console.log(`HTTP server listening on port ${PORT}`);
console.log(`(HTTPS redirection target set to port ${HTTPS_PORT})`);
console.log('Try visiting http://localhost:3000 (will redirect in production)');
});