redirect-https is a Node.js middleware designed for securing web applications by forcing HTTP traffic to HTTPS. It primarily functions by issuing a 301 (Moved Permanently) redirect for browsers while opting for an HTML meta refresh redirect for non-browser clients like APIs, bots, and `curl` requests. This deliberate choice aims to make developers aware of insecure HTTP usage, preventing silent failures often associated with direct 301s for programmatic access. The package is currently at version 1.3.1 and is a mature, stable utility with an infrequent release cadence, focusing on its core secure-by-default philosophy rather than extensive configurability. It is compatible with Express.js and standard Node.js `http` servers, offering options for custom body messages, proxy trust configuration, and specific path matching for advanced scenarios. Its key differentiator is the "secure-by-default" meta redirect strategy to surface security issues to developers.
npm install redirect-httpsVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to set up `redirect-https` with a standard Node.js HTTP server, forcing redirects from an insecure port to a secure one, and configuring `trustProxy` for reverse proxy setups.
If direct 301 redirects are desired for APIs or bots, configure the `apis` option: `require('redirect-https')({ apis: 301 })`. Consider the security implications before doing so.Enable the `trustProxy` option: `require('redirect-https')({ trustProxy: true })`. This makes the middleware inspect `X-Forwarded-Proto` headers to determine the client's original protocol.Prefer the default behavior or general options. Only use `paths` for very specific, well-understood edge cases, such as handling `curl | bash` installers that require a 301 on `/`.
If method-specific redirection is required, implement a custom middleware or conditional logic before `redirect-https`.
This is often the intended behavior of `redirect-https` to highlight insecure usage. To allow `curl` to receive a 301 redirect, configure the `apis` option: `redirector = require('redirect-https')({ apis: 301 });`.Ensure the `trustProxy` option is set to `true` when initializing `redirect-https` to correctly interpret `X-Forwarded-Proto` headers from the proxy: `redirector = require('redirect-https')({ trustProxy: true });`.Always prefer `{{ URL }}` or `{{ HTML_URL }}` which are safely escaped. Avoid using `{{ UNSAFE_URL }}` in user-facing content unless you have performed your own sanitization.No dependency data recorded yet.