Express HTTPS Redirect Middleware
raw JSON →express-force-https is an Express.js middleware designed to automatically redirect all incoming HTTP requests to their HTTPS equivalent. First published over a decade ago, its current (and only) stable version is 1.0.0. The middleware specifically checks if a request is already secure; if not, it issues a redirect. A key feature is its built-in exemption for `localhost` requests, preventing redirects during local development. Due to its age and lack of updates, it is considered abandoned and may not be suitable for modern Express applications, especially those deployed behind reverse proxies or load balancers which require specific `X-Forwarded-Proto` header handling. Alternative, more actively maintained solutions are generally recommended for production environments.
Common errors
error TypeError: Cannot read properties of undefined (reading 'secure') ↓
app.use(forceHttps) early in your middleware chain. error Application running on HTTP and not redirecting to HTTPS when deployed behind a proxy. ↓
app.set('trust proxy', 1); to your Express application. For more control, consider replacing express-force-https with custom middleware that checks req.headers['x-forwarded-proto'] === 'http'. error Local development (e.g., `http://localhost:3000`) is unexpectedly redirected to HTTPS. ↓
localhost. For other local development hostnames, explicitly exclude them in your proxy configuration or by adding a conditional check around the middleware in your development environment, e.g., if (process.env.NODE_ENV === 'production') { app.use(forceHttps); }. Warnings
breaking The package has not been updated in over 10 years and is considered abandoned. It may contain unpatched vulnerabilities or not function correctly with newer Node.js or Express.js versions. ↓
gotcha When running Express behind a reverse proxy (e.g., Nginx, AWS ELB, Heroku, Azure), the `req.secure` property might incorrectly report HTTP even if the client connected via HTTPS to the proxy. This requires configuring `app.set('trust proxy', 1)` in Express and often manually checking the `X-Forwarded-Proto` header. ↓
gotcha The default redirect status code used by `express-force-https` (which relies on Express's `res.redirect`) is typically 302 (Found). For permanent HTTPS enforcement and better SEO, a 301 (Moved Permanently) redirect is usually preferred. ↓
Install
npm install express-force-https yarn add express-force-https pnpm add express-force-https Imports
- secure wrong
import secure from 'express-force-https';correctconst secure = require('express-force-https');
Quickstart
const express = require('express');
const forceHttps = require('express-force-https');
const app = express();
// Use the forceHttps middleware.
// It should typically be one of the first middlewares to ensure all traffic is secured early.
app.use(forceHttps);
app.get('/', (req, res) => {
res.send('Hello from the Express server! This page should be served over HTTPS.');
});
app.get('/unsecure-test', (req, res) => {
res.send('You tried to access this via HTTP, but were redirected to HTTPS!');
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`HTTP server running on port ${PORT}. Try visiting http://localhost:${PORT}`);
console.log('You should be redirected to HTTPS if not on localhost and server is configured for SSL.');
});
// Note: This middleware only handles the redirect.
// You still need to set up an HTTPS server (e.g., with `https` module)
// or a reverse proxy (like Nginx) to handle incoming HTTPS requests.