HTTPS Server for Localhost

4.7.1 · maintenance · verified Sun Apr 19

https-localhost is a lightweight utility designed to quickly establish an HTTPS server on localhost, featuring HTTP/2 and SSL via locally-trusted development certificates. It simplifies local development by eliminating the need for manual certificate generation and trust setup, supporting MacOS, Linux, and Windows, and working seamlessly with Chrome and Firefox. The current stable version is 4.7.1, with minor updates released periodically. It functions both as a standalone command-line tool for serving static files and as an importable module for Express.js applications or other web frameworks, offering unique ease-of-use for secure local development environments. Its key differentiator is the zero-configuration approach to trusted local SSL, integrating with `mkcert` under the hood. The project is currently seeking maintainers and contributors.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to initialize an HTTPS server with Express.js, serve static files, and obtain generated certificates for custom use, utilizing environment variables for port configuration.

const httpsLocalhost = require("https-localhost");
const app = httpsLocalhost(); // Creates an Express app with local HTTPS
const port = process.env.PORT || 4433; // Default to 443 for HTTPS, using 4433 to avoid root permissions

// Create a 'public' directory and add some static files for testing
// e.g., echo '<h1>Hello, HTTPS!</h1>' > public/index.html

// Serve static files from a 'public' directory
// Ensure 'public' directory exists in your project root for this to work
app.serve(`${__dirname}/public`);

// Optionally redirect HTTP traffic to HTTPS (on the same port by default)
// app.redirect();

app.listen(port, () => {
  console.log(`HTTPS server listening on https://localhost:${port}`);
  console.log(`Serving static files from ${__dirname}/public`);
});

// Example of creating certificates for an additional domain and using them directly
async function demonstrateCustomCerts() {
  // Pass a domain string to the main function to generate certificates for it
  const customHttpsServer = httpsLocalhost("mycustomdomain.com");
  try {
    const customCerts = await customHttpsServer.getCerts();
    console.log("\nCustom certificates generated for mycustomdomain.com:");
    console.log("  Certificate present:", !!customCerts.cert);
    console.log("  Key present:", !!customCerts.key);
    // You can now use customCerts with Node's native https.createServer
    // Example: https.createServer(customCerts, someOtherApp).listen(8443);
  } catch (error) {
    console.error("Error generating custom certificates:", error.message);
  }
}
demonstrateCustomCerts();

view raw JSON →