hapi-require-https is a Hapi plugin designed to enforce HTTPS for incoming requests, providing automatic HTTP to HTTPS redirection. The current stable version is 6.0.0, which requires Hapi v20 or newer. This plugin primarily operates by default using the `X-Forwarded-Proto` header, making it ideal for applications running behind a reverse proxy (like on Heroku or other PaaS environments). It offers a straightforward API, registering as an `onRequest` lifecycle hook to perform 301 redirects. A key differentiator is its explicit support for proxy environments, configurable via a `proxy` option, which can be set to `false` to redirect based on the actual request protocol instead of the forwarded header. Release cadence typically aligns with major Hapi versions or necessary compatibility updates.
npm install hapi-require-httpsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to set up a basic Hapi server and register hapi-require-https, enabling automatic HTTP to HTTPS redirection. It includes a simple route to show the active protocol.
Upgrade your Hapi server to `@hapi/hapi@^20.0.0` or higher, or downgrade `hapi-require-https` to a compatible major version (e.g., `5.x` for Hapi v19).
If your application is not behind a reverse proxy, set the `proxy` option to `false` when registering the plugin: `server.register({ plugin: HapiRequireHttps, options: { proxy: false } })`. This forces redirection based on the actual request protocol.Prefer `import HapiRequireHttps from 'hapi-require-https';` for new projects or when using ESM. For existing CommonJS projects, continue using `const HapiRequireHttps = require('hapi-require-https');` but monitor for future compatibility issues.Upgrade your Hapi installation: `npm install @hapi/hapi@^20` or `yarn add @hapi/hapi@^20`. Alternatively, downgrade hapi-require-https to a version compatible with your current Hapi installation, e.g., `npm install hapi-require-https@^5`.
Verify the order of plugin registration. hapi-require-https should generally be registered before other plugins that might send responses or perform complex operations that could interfere with early redirection.